Thursday, 4 December 2014

Inheritance

Inheritance

* Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

 *The idea behind inheritance in java is that you can create new classes that are built upon existing classes.

*When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.

 *Inheritance represents the IS-A relationship, also known as Parent-Child relationship.

 *Used for Method Overriding (So Runtime Polymorphism).

 *User for Code Reusability.

 *The keyword extends indicates that you are making a new class that derives from an existing class.

 *In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.

public class A { 
 int a = 10;
 int add (){
 return  a = a+10; 
 }
} 
class B extends A { 
 public static void main(String args[]){  
  B b = new B();
  b.a = 20;
  System.out.println(b.add());    
 } 
}

DOWNLOAD INHIRITANCE EXAMPLE

No comments: