Professional Documents
Culture Documents
Inheritance
Inheritance is one of the cornerstones of objectoriented programming.
A class that is inherited is called a superclass. The
class that does the inheriting is called a subclass.
A subclass is a specialized version of a superclass.
extends is the keyword used to inherit a class.
The general form of a class declaration :
class subclass-name extends superclass-name {
// body of class
}
Inheritance - Example
class Base
{
int i, j;
void showij()
{
System.out.println("i and j in Base: " + i + " & " + j);
}
}
class Sub extends Base
{
void sum()
{
System.out.println( i+j in Sub: " + (i+j));
}
}
Inheritance - Example
class SimpleInheritance
{
public static void main(String args[])
{
Sub subOb = new Sub();
subOb.i = 10;
subOb.j = 15;
subOb.showij();
subOb.sum();
}
}
super - Keyword
super has two general forms.
The first calls the superclass constructor.
The second is used to access a member of the superclass
that has been hidden by a member of a subclass.
Method Overriding
In a class hierarchy, when a method in a subclass
has the same name and type signature as a method
in its superclass, then the method in the subclass is
said to override the method in the superclass.
To access the superclass version of an overridden
function, you can do so by using super.
Method overriding occurs only when the names and
the type signatures of the two methods are identical.
DMD - Example
class X
{
void callme()
{
System.out.println("Inside X's callme method");
}
}
class Y extends X
{
void callme()
{
System.out.println("Inside Y's callme method");
}
}
class Z extends Y
{
void callme()
{
System.out.println("Inside Z's callme method");
}
}
DMD - Example
class Dispatch
{
public static void main(String args[])
{
X x = new X();
Y y = new Y();
Z z = new Z();
X r; // Reference of type X
r = x;
r.callme();
r = y;
r.callme();
r = z;
r.callme();
}
}
Abstract class
To declare an abstract method, the general form is :
abstract type name(parameter-list);
Interface
Interfaces are syntactically similar to classes, but
they lack instance variables, and their methods are
all abstract methods.
A class can implement any number of interfaces.
Each class that implements an interface must define
all of the methods declared in that interface.
When you implement an interface method, it must
be declared as public.
Variables declared inside an interface are implicitly
final and static.
Interface - Example
interface InterOne
{
void implementMe();
}
interface InterTwo
{
void implementMeToo();
}
Interface - Example
class Client implements InterOne, InterTwo
{
public void implementMe()
{
System.out.println("Implement One");
}
public void implementMeToo()
{
System.out.println("Implement Two");
}
void nonInterfaceMeth()
{
System.out.println("Client Method");
}
}
Interface - Example
class TestInterface
{
public static void main(String args[])
{
Client clOb = new Client();
clOb.implementMe();
clOb.implementMeToo();
clOb.nonInterfaceMeth();
}
}
Packages
Package is a mechanism for partitioning the class name
space into more manageable chunks.
The package is both a naming and a visibility control
mechanism.
You can define classes inside a package that are not
accessible by code outside that package.
To create a package, include a package command as the
first statement in a Java source file.
package MyPackage;
Package - Example
package MyPackage;
public class MyPack
{
public int add(int a, int b)
{
System.out.println(My Package's Method);
return a + b;
}
}
Package - Example
import MyPackage.MyPack;
public class TestPack
{
public static void main(String args[])
{
MyPack packOb = new MyPack();
int val = packOb.add(10,10);
System.out.println("Value is " + val);
}
}
Access Specifier
Access Levels
No-modifier
protected
public
Same Class