Chap 4, Interf - Pack
Chap 4, Interf - Pack
>> Used for accessing the static variables and >> object_name.method_name();
>> can be invoked using class name static methods can access only static
-- object reference can also be used variables and other static methods
cannot access the non static
class Demo{
final int MAX_VALUE=99;
void myMethod(){
MAX_VALUE=101;
}
public static void main(String args[]){
Demo obj=new Demo(); obj.myMethod();
}}
Extends any number of Extends only one class Extends only one class
interfaces
Cannot be instantiated Cannot be instantiated Can be instantiated
Implementing an interface is like signing a contract with the compiler that states “ I will
define all the method specified by the interface or I will declare my class abstract”
Extending Interfaces
When implementation interfaces there are several rules:
A class can implement more than one interface at a time.
A class can extend only one class, but implement many interfaces.
An interface can extend another interface, similarly to the way that
a class can extend another class.
The extends keyword is used to extend an interface, and the child
interface inherits the methods of the parent interface.
<<access specifier>>interface<<interfaceName>> extends
<<super_interfaceName>> {}
Cont..
Extending Multiple Interfaces:
A Java class can only extend one parent class.
Multiple inheritance is not allowed.
Interfaces are not classes, however, and an interface can extend
more than one parent interface.
The extends keyword is used once, and the parent interfaces are
declared in a comma-separated list.
<<access specifier>>interface<<interfaceName>> extends
<<super_interfaceName>> , <<super_interfaceName2>> {}
Why do we use Interfaces?
To have unrelated classes implement similar methods
Example:
Class PDF and Music
– Not related
– Both implements download methods
-- isDownload
To model multiple inheritance which allows a class to have
more than one superclass
Sample code
public interface Relation class music implements Relation
{ {
public boolean isDownload()
public boolean isDownload();
{
}
System.out.println(“PDF download”);
class PDF implements Relation
}
{
}
public boolean isDownload() Class test{
{ public static void main(String[] args) {
System.out.println(“PDF download”); PDF p= new PDF();
} p.isDownload();
} }}
Packages
Sub-directories within which related classes can be placed together
for organization.
Group of classes placed within folder(s).
Example: package transportation can contain classes Airplane, Train,
Car, Boat, Bike
To make a class a part of a package transportation we have to write
“package transportation” at the first line of code
[you cannot write any java code before package name]
packages can have sub package.
Example: java.awt [awt is a sub package in the java package]
Cont..
Importing a particular class
One declared that a class belongs to some package, the actual name of
the class is packageName.className
Example: transportation.Car is the actual name for class Car
To import anywhere in your code use: import transportation.Car
[ this for if you need to use the class Car in other package, not in the same package ]
Importing a package
Importing a package means importing the whole classes of the package.
Implicit imports
The class of java.lang are automatically imported.
Example: Math class and system class are directly used without any import
statements.