packages n interfaces

31
Packages and Interfaces

Upload: anupam-srivastava

Post on 04-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 1/31

Packages and

Interfaces

Page 2: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 2/31

Packages

• To compartmentalize class names, stored in ahierarchical manner. E.g. io package is in java

package.

• collection of classes.

• Naming and visibility control mechanism.

• Source file components:

(a) Package statement (optional)

(b) Import statements. (optional)

(c) Single public class declaration. (essential)

(d) Classes private to the package. (optional)

Page 3: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 3/31

Package Use

• Suppose directory “gs” exists and two peoplewant to create two source files by their name.

• Problem is: their name is same say “shobhit” 

• Solution is to create two folders under gs say p1and p2 and ask each person to choose a

directory p1 or p2.

• The fully qualified name for each program

becomes p1.shobhit and p.shobhit.

• The directories p1 and p2 are synonymous to

package p1 and p2 and the source files belong

to either p1 or p2.

Page 4: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 4/31

Syntax for declaring a package

• Package declaration should be the firststatement in the source file.

• Syntax: package somename;

•  A source file belonging to some package must

be stored under a directory with the name being

the same as that of the package (Case

Sensitive).

• Multilevel package declaration syntax:package p1.p2.p3;

Here p3 is inside p2 and p2 is inside p1 and our

source file is inside p3.

Page 5: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 5/31

Finding packages and CLASSPATH

• By default, JR system uses currentworking directory as it’s starting point forfinding the package.

• Directory path/paths can be specified byusing CLASSPATH environment varaible.

• Example: to execute program withpackage stmt. Package MyPack; program

must be executed from a directoryimmediately above MyPack orCLASSPATH must include path toMyPack.

Page 6: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 6/31

Example of package

package Gift;

class ScoobyDoo{

public static void main(String args[])

{

System.out.println("Java2 ab to aaja");}

}

Page 7: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 7/31

E:\javaprogs\Gift>javac ScoobyDoo.java

E:\javaprogs\Gift>

E:\javaprogs\Gift>java ScoobyDoo

Exception in thread "main" java.lang.NoClassDefFoundError: ScoobyDoo(wrong name

: Gift/ScoobyDoo)

• E:\javaprogs>java Gift.ScoobyDoo

Java2 ab to aaja

• E:\>java Gift.ScoobyDoo

Exception in thread "main" java.lang.NoClassDefFoundError: Gift/ScoobyDoo 

Page 8: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 8/31

Setting CLASSPATH variable

• Go to MyComputer properties 

 Advanced Environment variables 

Select CLASSPATH Edit Add the

required path For example: e:\javaprogs;

• If this done then last case in previous slide

i.e.

• E:\>java Gift.ScoobyDoo

executes successfully.

Page 9: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 9/31

 Access protection

Private Default Protected Public

Same class Yes Yes Yes Yes

Same

package

subclass

No Yes Yes Yes

Same

package non-

subclass

No Yes Yes Yes

Different

packagesubclass

No No Yes Yes

Different

package non-

subclass

No No No Yes

Page 10: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 10/31

Complete Example

• Define 2 packages p1 and p2 inside

E:\javaprogs

• CLASSPATH includes E:\javaprogs

• Create 4 source files in p1.

• Create 3 source files in p2

• Use variables of p1 in p2.

Page 11: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 11/31

package p1;

public class Protection {

int n = 1;

private int n_pri = 2;

protected int n_pro = 3;

public int n_pub = 4;

public Protection() {

System.out.println("base constructor");

System.out.println("n = " + n);

System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub);

}

}

Page 12: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 12/31

package p1;

class Derived extends Protection {

Derived() {

System.out.println("derived constructor");

System.out.println("n = " + n);

// class only

// System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub); } }

Page 13: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 13/31

package p1;

class SamePackage {

SamePackage() {Protection p = new Protection();

System.out.println("same package constructor");

System.out.println("n = " + p.n);// class only

// System.out.println("n_pri = " + p.n_pri);

System.out.println("n_pro = " + p.n_pro);

System.out.println("n_pub = " + p.n_pub);

}

}

Page 14: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 14/31

package p1;

// Instantiate the various classes in p1.

public class Demo {

public static void main(String args[]) {

Protection ob1 = new Protection();Derived ob2 = new Derived();

SamePackage ob3 = new SamePackage();

}

}

Page 15: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 15/31

package p2;

class Protection2 extends p1.Protection {

Protection2() {System.out.println("derived other package constructor");

// class or package only

// System.out.println("n = " + n);// class only

// System.out.println("n_pri = " + n_pri);

System.out.println("n_pro = " + n_pro);

System.out.println("n_pub = " + n_pub);

}

}

Page 16: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 16/31

package p2;

class OtherPackage {

OtherPackage() {p1.Protection p = new p1.Protection();

System.out.println("other package constructor");

 // class or package only

 // System.out.println("n = " + p.n);

 // class only

 // System.out.println("n_pri = " + p.n_pri);

 // class, subclass or package only

 // System.out.println("n_pro = " + p.n_pro);

System.out.println("n_pub = " + p.n_pub);

} }

Page 17: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 17/31

package p2;

// Instantiate the various classes in p2.

public class Demo {

public static void main(String args[]) {

Protection2 ob1 = new Protection2();

OtherPackage ob2 = new OtherPackage();

}

}

Page 18: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 18/31

• Execute Demo class in p2 like this:

• Step 1) e:\Javaprogs\p2> javac Demo.java

• Step 2) e:\Javaprogs\p2> cd ..

• Step 3) e:\Javaprogs>java p2.Demo

• The following out put appears:• base constructor

• n = 1

• n_pri = 2

• n_pro = 3

• n_pub = 4• derived other package constructor

• n_pro = 3

• n_pub = 4

• base constructor

• n = 1• n_pri = 2

• n_pro = 3

• n_pub = 4

• other package constructor

• n_pub = 4

Page 19: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 19/31

Some salient points

• Unlike the instance variables classes haveonly two access specifiers: default andpublic.

•  A class must be declared as public if

imported/used outside it’s own package. • There can only be a single public class

declaration within a source file. (may/may

not contain main)• The constructor of a public class may/may

not be declared as public.

Page 20: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 20/31

Importing packages•  All inbuilt classes in Java are placed within some

package.

• Package at the highest level is named “java”. 

• To use a class without the fully qualified name,we use import stmt.

• Syntax: import p1.[p2.p3.]classname|*;

• * increases compilation time but not executiontime.

•  java.lang.*; automatically imported in program.

• If using import then it must the statement justafter the package declaration.

• When importing packages only those itemswithin package declared as public will beavailable to non-subclasses.

Page 21: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 21/31

 Another good example 

package p1;

public class A

{

private int n1 = 1;

protected int n2 = 2;

int n3 = 3;

public int n4 = 4;

}

Page 22: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 22/31

package p2;

import p1.A; //import p1.*;

class B extends A

{

public static void main(String args[]){

A obj = new A();

 //System.out.println(obj.n1);

 //System.out.println(obj.n2);

 //System.out.println(obj.n3);System.out.println(obj.n4);

B obj1 = new B();

 //System.out.println(obj1.n1);

System.out.println(obj1.n2); //System.out.println(obj1.n3);

System.out.println(obj1.n4);

}

}

Page 23: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 23/31

Interfaces

• Syntactically similar to classes but lack instancevariables and their methods are declared without

a body.

• Once defined any number of classes can

implement an interface.

• One class can implement several interfaces.

• To implement a class must implement all

methods in an interface.• This gives rise to form of multiple inheritance.

• They are preferred over abstract classes.

Page 24: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 24/31

Syntax of Interface

• Syntax:

access interface name{

ret_type method1(paramlist);

ret_type method2(paramlist);

type fin_sta_varname1 = value;

type fin_sta_vaname2 = value;

}

access = public or default.

If interface public then allmethods/variables inside are public.

Page 25: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 25/31

Implementing an interface

• Syntax:

access class classname [extends

Superclass] [implements interface i1, i2,

i3]

{

//class Body

}

Page 26: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 26/31

Page 27: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 27/31

Example of interface

interface X{

void method();

int x = 40;

int c = 90;}

interface Y

{void method();

int x = 60;

}

Page 28: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 28/31

class Inter implements X, Y

{

public void method()

{

System.out.println("HI");}

public static void main(String args[])

{

System.out.println(X.x); //40

System.out.println(Y.x); //60

System.out.println(c); //90

X obj1;

 Y obj2;

Inter in = new Inter();

in.method(); //HI

obj1 = in;obj1.method(); //HI

obj2 = in;

obj2.method(); // HI

}

}

Page 29: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 29/31

Extending interfaces

interface A

{

void f1();

}

interface B extends A

{

void f2();}

Page 30: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 30/31

class ExIn implements A,B

{

public void f1()

{System.out.println("A");

}

public void f2()

{ System.out.println("B");

}

public static void main(String args[])

{

ExIn obj = new ExIn();obj.f1();

obj.f2();

}

}

Page 31: Packages n Interfaces

8/13/2019 Packages n Interfaces

http://slidepdf.com/reader/full/packages-n-interfaces 31/31

There is alwaysroom at the top.Chapter ends.