Inheritance in Java

 

Corejava InheritanceNotes

coding-zon-java-inheritance



 

 

 

 


 
 

 Table of contents

 
 
 
 
 

Defination: 

Inheritance is one of the key features of Object-oriented programming. It is the mechanism in java which allows us to create a new class (derived class) from an existing class (base class).

The derived class inherits the features (attributes and methods) from the base class and can have additional features of its own.

 
Example:
 
class A{
void m1(){
	System.out.print ("m1");
}
}
class B extends A{
void m2(){
	System.out.print ("m2");
}
}
class Test{
public static void main(String args){
	A aob= new A();
	B aob = new B();
	aob.m1();
	bob.m2();

             bob.m1(); // inherited from A 

}

}

 
 

 

super  keyword

 

Used to invoke parent class constructor from the child class, to pass parameters from the child constructor to parent constructor.

 
ex:
 

class A
    int x;
A(int x)
{
    this.x=x;
}
void area() 
{ 
    int asq = x * x;
    System.out.println(asq);
}
} 
class B extends A
    int y,z;
B(int i, int j, int k ) { 
    super(i); //invokes A's constructor and (i is passed to x) 
    y = j; 
    z = k;
}
area() {
    int ar = y * z; 
    System.out.println(ar); 
	}	

 }
 
 

In the above, ex. B can be extended only one class

It cannot be extended by more than one class, ex:

class B extends A, C  //not allowed so

 

Multiple inheritance can be achieved by Interfaces.

 

 

 

 Interfaces in Java

 
 


Interface contains only method declarations(empty methods). 

These methods are implemented by other classes 


Interface declaration

interface Iface1{
 m1();
 m2();
 m3();
}

 

Interface implementation

 

interface Iface1{

 m1();
 m2();
 m3();
}
class A implements Iface1{
m1(){
....; 
....; 
}
m2()
{ 
....; 
....;
}
m3()
{ 
....; 
....;
}
 public static void main(String args{})
 
 A aobj = new A();  
 
aobj .m1();  
aobj .m2();  
aobj .m3();
} 

 }
 
 
 
 
ex2: 

implementing multiple interfaces 

 
interface Iface1
{ 
v1; 
m1(); 
}
interface Iface2{ 
v2; 
m2(); 
}
class A implements Iface1,Iface2 {
m1(){
....; 
....; 
}
m2()
....; 
....;
}
 }
 

 

example2 

interface Shape{
 
 void area();
}
 
class square implements Shape { area(){.... }
}
class Rect implements Shape { area(){.... }
}
class Circle implements Shape { area(){.... }
}

-- in the above ex. area() is common in all classes(Square, Rect, Circle).

 Uses of Interfaces: 

To achieve commonality, i.e. to maintain some standard among different classes, interfaces are used.


 

Rule for implementing interfaces

  • All Methods must be defined in the implementation class.
  • If not defined all methods, then that class becomes abstract. (means not complete) this class can't be instantiated. So this class must be extended by some other class which implements all remaining  methods of interface
  • Then this child class called as Concrete Class (this class can be instantiated).
  • Abstract classes extended by some other classes

 

 

Abstract class in Java

 
  • Abstract class contains some methods undefined.
  • Abstract class can't be instantiated.
 
ex: 
 
abstract class A 
 
m1(); // method declaration. 
 
m2(){ 
.......; 
.......; 
 

---m1()  // not defined just it is declared.

-- extended class will provides definition for method declarations.

 
ex:
 
class B extends A
{
m1();  // A's Method defined
{
.......; 
}
m3()  // B's own method.
{ 
.......; 
}
}

 

Concrete class

The class  which defines all the methods of its parents is called concrete class. Concrete class can be instantiated. In the above example,  class B can be concrete class.

 ex:

class B extends A{
m1() // A's Method defined 
.......; 
}
m3() // B's own method.
.......;
}
public static void main(String args{})
B b= new B(); 
b.m1(); 
b.m2(); 
b.m3();
} 

Overriding in Java Inheritace

 Base class method is redefined in the derived class.

Rules:  
  • Both methods should have same signature i.e., both method name, parameters list should be same. 
  • return type can be different. 
  • method which is invoked depends on the object created.

 ex:

class Animal{
	eat() {
	s.o.p("eats something"); }
class Horse extends Animal { 

eat(){
	s.o.p("oates"); }
	}	
 }
 
class OverrideDemo{
 
public static void main(String args[]) { 

Animal a= new Animal();// o/p -->something 

Animal b= new Horse();// o/p -->oates 

a.eat(); 

b.eat(); 

}}
 
 
program: 
 
public class Animal{
void eat() {
System.out.println("eats something"); 
} 
}
class Horse extends Animal { 
void eat(){ 
System.out.println("eats oates");
}
}
class OverrideDemo{
public static void main(String args[]) 
{ 
Animal a= new Animal();//-----> o/p -->eats something 
		
Animal b= new Horse();//-----> o/p -->eats oates
a.eat(); 
		
b.eat(); 
}
}
	

 

Menu

Dynamic Binding Vs static binding in Java

static binding

Reference points to its own type of object.

Rect rt = new Rect(); 

 

dynamic binding

It is super type of reference, points to any of its child object  

ex: 

Shape sp; 

sp = new Square();  //or

sp = new Rect();   //or

sp = new Circle(); 

sp = rt   // assining Rect ref to its super class ref

 
example 
 
interface Shape{
	area();
}
class square implements Shape { 
	area(){
	....; 
	}
	}
class Rect implements Shape 
{ 
	area(){
	....; 
	}
}
class Circle implements Shape 
     area(){
         .... ;
         }
}
class Test{
	Shape sp; 
	Square sq= new Square(); 
	sq.area();
	Rect rt = new Rect();//static binding occurs at compile time[compiler] 
	rt.area(); 
	Circle cr= new Circle(); 
	cr.area();
	sp=rt; sp.area(); //Dynamic Binding occurs at runtime[jvm]
}
}	
 
 
 
example program 
 

interface Shape{
	public float area();
}
class Square implements Shape { 
	int side;
	Square(int side) 
	{  
        this.side=side;
	}
	public float area(){
		return (side*side);
	}
}
class Rect implements Shape 
{ 
	int side; 
int width;
	Rect(int side, int width)
	{  
		this.side=side; 
		this.width=width; 
	}
	public float area()
	{ 
		return (side*width);
	}
}
class Circle implements Shape 
{ 
	public final float pi=3.14f; 
	int r; 
	Circle(int r)
	{ 
		this.r=r; 
	}
	public float area(){
		return (2*pi*r*r); 
	}
}
class Test1{ 
 
public static void main(String args[]){ 
 
       float area = 0.0f; 
	Square sq = new Square(4); 
	area = sq.area();
	System.out.println("Square Area="+area); 
 
       Rect rt = new Rect(5,8); 
	area = rt.area();
	System.out.println("RectangleArea="+area); 
 
       Circle cr = new Circle(5); 
	area = cr.area();
	System.out.println("Circle Area="+area); Shape sp; 
 
       sp = sq; 
	area = sp.area(); //Dynamic Binding occurs at runtime[jvm]
	System.out.println("Area="+area); 
}
} 

 

 

 

 

Types of interitance in Java 

coding-zon-types-of-interitance-in-Java

Extends Vs implements


coding-zon-java-extend-vs-implementation



 Java Tutorial Home

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP