Chapter 9
Chapter 9
oop\amp\btech\packages
Package and CLASSPATH
How does the Java run-time system know where to look for
packages that you create?
First, by default, the Java run-time system uses the current
working directory as its starting point. Thus, if your package is in a
subdirectory of the current directory, it will be found.
Package and CLASSPATH
Package and CLASSPATH
Package and CLASSPATH
How does the Java run-time system know where to look for
packages that you create?
Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
Package and CLASSPATH
How does the Java run-time system know where to look for
packages that you create?
Third, you can use the –classpath option with java and javac to
specify the path to your classes.
Access Protection
Access Protection
While Java's access control mechanism may seem complicated,
we can simplify it as follows.
Anything declared public can be accessed from anywhere.
Anything declared private cannot be seen outside of its class.
When a member does not have an explicit access specification,
it is visible to subclasses as well as to other classes in the same
package. This is the default access.
If you want to allow an element to be seen outside your current
package, but only to classes that subclass your class directly,
then declare that element protected.
Reference:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Access Protection
Access Protection
Access Protection
Access Protection
Access Protection
Importing Packages
In java all of the standard classes are stored in some named
package.
If we need use some classes from different package we should
import that package using import statement
Once imported, a class can be referred to directly, using only its
name.
To specify either an explicit classname or a star (*), which
indicates that the Java compiler should import the entire
package.
import pkg1[.pkg2].(classname|*);
import java.util.Date;
import java.io.*;
Importing Packages
Importing Packages
TYPES OF INHERITANCE
TYPES OF INHERITANCE
Multiple inheritance is not supported in java through class.
Interfaces
Using interface, we can fully abstract a class' interface from its
implementation.
Using interface, we can specify what a class must do, but not
how it does it.
Interfaces are syntactically similar to classes, but they lack
instance variables, and their methods are declared without any
body.
Once it is defined, any number of classes can implement an
interface.
One class can implement any number of interfaces.
To implement an interface, a class must create the complete set
of methods defined by the interface.
Each class is free to determine the details of its own
implementation
Why interface?
Interfaces are designed to support dynamic method
resolution at run time.
Normally, in order for a method to be called from one
class to another, both classes need to be present at
compile time so the Java compiler can check to ensure
that the method signatures are compatible. This
requirement by itself makes for a static and non
extensible classing environment.
Defining interface
Output:
INTERFACES CAN BE
EXTENDED
One interface can inherit another by use of the keyword
extends. The syntax is the same as for inheriting
classes.
When a class implements an interface that inherits
another interface, it must provide implementations for
all methods defined within the interface inheritance
chain
// One interface an extend another.
interface A {
void meth1(); public void meth3() {
void meth2(); System.out.println("Implement
} meth3().");
// B now includes meth1() and meth2()- }
// it adds meth3(). }
interface B extends A {
void meth3(); class IFExtend {
} public static void main(String arg[]) {
MyClass ob = new MyClass();
// This class must implement all of A and B
class MyClass implements B { ob.meth1();
public void meth1() { ob.meth2();
System.out.println("Implement ob.meth3();
meth1()."); }
} }
public void meth2() {
System.out.println("Implement
meth2().");
}