Escolar Documentos
Profissional Documentos
Cultura Documentos
1. Class Inheritance
2. Interface Inheritance
Class Inheritance
1. It is used to create a new class as an extension of
another class.
2. Its purpose is of Code Reuse
3. The derived class inherits the public methods and
public data of the base class.
Interface
Inheritance
1)Create a new class to implement the methods
defined as part of an interface for the purpose of
subtyping.
2)Java supports multiple inheritance.
3)In Java Multiple Inheritance can be achieved
through use of Interfaces by implementing more
than one interfaces in a class.
Examples
Class Inheritance
Public class derived-class-name extends base-class-name
{
Interface Inheritance
public class class-name implements interface-name
{
}
Forms of
Inheritance
1) Single Inheritance
2)Multiple Inheritance
3)Hierarchical Inheritance
4)Multilevel Inheritance
Single Inheritance
When a subclass is derived simply from it's parent
class then this mechanism is known as simple
inheritance. In case of simple inheritance there is
only a sub class and it's parent class. It is also
called single inheritance or one level inheritance
Example of Single
Inheritance
class A {
int x;
int y;
int get(int p, int q){
x=p; y=q; return(0);
}
void Show(){
System.out.println(x);
}
}
class B extends A{
public static void main(String args[]){
A a = new A();
a.get(5,6);
a.Show();
}
void display(){
System.out.println("B");
Multilevel
Inheritance
It is the enhancement of the concept of inheritance.
When a subclass is derived from a derived class then
this mechanism is known as the multilevel inheritance.
The derived class is called the subclass or child class
for it's parent class and this parent class works as the
child class for it's just above ( parent ) class.
Multilevel inheritance can go up to any number of
level.
Example of Multiple
Inheritance
class A {
int x;
int y;
int get(int p, int q){
x=p; y=q; return(0);
}
void Show(){
System.out.println(x);
}
}
class B extends A{
void Showb(){
System.out.println("B");
}
}
class C extends B{
void display(){
System.out.println("C");
}
public static void main(String args[]){
A a = new A();
a.get(5,6);
a.Show();
}
}
Super Keyword
As the name suggest super is used to access the
members of the super class. It is used for two
purposes in Java.
public static void main(String[] args){
B subobj = new B(1, 5);
subobj.Show();
}
}