Monday, June 27, 2016

Inheritance Java Example

We can acquire all the members of a class and use them in another class by relating the objects of two class .This is possible by inheritance.

What is Inheritance?

Deriving new classes from existing classes such that the new classes acquire all the features of existing classes is called Inheritance.

So,inheritance is a concept where new classes can be produced from existing classes.the new created class acquires all the features of existing class from where it is derived.

Sample Program:

/**
 * @author Abhinaw.Tripathi
 *
 */
class Teacher
{
  int id;
  String name;
  String address;
  float sal;

  public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}

}

class Student
{
int id;
String name;
String address;
int marks;

 public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}

}

public class InheritanceApp
{
public static void main(String[] args)
{
Student s=new Student();
s.setName("Abhinaw");
s.setId(1001);
s.setAddress("Dlf Phase 1");
s.setMarks(50);

System.out.println("Student name="+s.getName());
System.out.println("Student Id="+s.getId());
System.out.println("Student address="+s.getAddress());
System.out.println("Student marks="+s.getMarks());
}


}


Result:

Student name=Abhinaw
Student Id=1001
Student address=Dlf Phase 1
Student marks=50

Another sample Program:

/**
 * @author Abhinaw.Tripathi
 *
 */
class Teacher
{
  int id;
  String name;
  String address;
  float sal;
  
  public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}

}

class Student extends Teacher
{
int marks;
public int getMarks()
{
return marks;
}
public void setMarks(int marks)
{
this.marks = marks;
}
}

public class InheritanceApp
{
public static void main(String[] args) 
{
Student s=new Student();
s.setName("Abhinaw");
s.setId(1001);
s.setAddress("Dlf Phase 1");
s.setMarks(50);
System.out.println("Student name="+s.getName());
System.out.println("Student Id="+s.getId());
System.out.println("Student address="+s.getAddress());
System.out.println("Student marks="+s.getMarks());
}

}

Result:

Student name=Abhinaw
Student Id=1001
Student address=Dlf Phase 1
Student marks=50

Why super class members are available to sub class?

Ans: Because,the sub class object contains a copy of super class object.then what is the advantage of inheritance?you can clearly see the second program was smaller than the first one.

The Keyword 'super'

If we create an object to super class ,we can access only the super class members, but not the sub class members. but if we create sub class object ,all the members of both super and sub class are available to it. This is the reason,we always create an object to sub class in inheritance.Some time the super class members and sub class members may have same names.In that case,by default only sub class members are accessible .

Sample Program:

/**
 * @author Abhinaw.Tripathi
 *
 */
class one
{
   int i=20;
   void show()
   {
  System.out.println("Super class Method:i " +i);
   }
}

class two extends one
{
int i=20;
void show()
  {
  System.out.println("Sub class Method: i" +i);
  } 
}

public class InheritanceSuperDemo 
{
public static void main(String[] args)
{
two t=new two();
t.show();
}

}

Result:

Sub class Method: i20

Another Sample Program using Super:

/**
 * @author Abhinaw.Tripathi
 *
 */
class one
{
   int i=20;
   void show()
   {
  System.out.println("Super class Method:i= " +i);
   }
}

class two extends one
{
int i=20;
void show()
  {
  System.out.println("Sub class Method: i" +i);
  super.show();
  System.out.println("super i= "+super.i);
  } 
}

public class InheritanceSuperDemo 
{
public static void main(String[] args)
{
two t=new two();
t.show();
}

}

Result:

Sub class Method: i20
Super class Method:i= 20
super i= 20

Another Sample Program using default constructor:

/**
 * @author Abhinaw.Tripathi
 *
 */
class one
{
   
   public one() 
   {
System.out.println("One");
   }
}

class two extends one
{
public two()
{
System.out.println("two");
}
}

public class InheritanceSuperDemo 
{
public static void main(String[] args)
{
two t=new two();
}

}

Result:

One
two

Another Program:

/**
 * @author Abhinaw.Tripathi
 *
 */
class one
{
   int i;
   
   public one(int a) 
   {
this.i=a;
   }
   
}

class two extends one
{
int i;
    public two(int a,int b)
    {
super(a);
i=b;
}
void show()
  {
  System.out.println("Sub class Method: " +i);
  System.out.println("super class i="+super.i);
  } 
}

public class InheritanceSuperDemo 
{
public static void main(String[] args)
{
 two t=new two(11, 12);
 t.show();
}

}

Result:

Sub class Method: 12
super class i=11


The Protected Specifier

The private members of the super class are not available to sub classes directly.But some times,there may be a need to access the data of super class in the sub class .For this purpose ,protected specifier is used.protected  is commonly used in super class to make the members of the super class available directly in its sub classes.We can think that the protected specifier works like public with respect to sub classes .

Sample Program:

/**
 *
 */
package com.inheritanceclass;

/**
 * @author Abhinaw.Tripathi
 *
 */
class Access
{
  private int a=10;
  protected int b=20;

}

class subClass extends Access
{
  public void get()
  {
//   /System.out.println(a); // error ---a is private
  System.out.println(b);
  }
}

public class ProtectedSpeInheritanceApp
{
/**
* @param args
*/
public static void main(String[] args)
{
 subClass sc=new subClass();
 sc.get();
}

}

Result:  20


Another Sample Program:

/**
 * 
 */
package com.inheritanceclass;

/**
 * @author Abhinaw.Tripathi
 *
 */
class Shape
{
 protected double l;
 
 public Shape(double l) 
 {
this.l=l;
 }
  
}

class Square extends Shape
{

public Square(double l) 
{
super(l);
}
void area()
{
System.out.println("Area of square:" + (l*l));
}
}


class Rectangle extends Square
{
    private double b;
public Rectangle(double x,double y) 
{
super(x);
b=y;
}
void calculateArea()
{
System.out.println("Area of rectangle = "+(l*b));
}
}

public class AnotherSampleProgInh
{

public static void main(String[] args)
{
      Square s=new Square(5.5);
      s.area();
      
      Rectangle rectngl=new Rectangle(5.5, 6);
      rectngl.calculateArea();
}
}

Result:

Area of square:30.25
Area of rectangle = 33.0

Types of Inheritance

There are two types of inheritance,Single and Multiple
  1. Single Inheritance: Producing sub classes from a single super class is called Single Inheritance.in this case,a single super class will be there.there can be one or more sub classes.     
  2. Multiple Inheritance: Producing sub classes from multiple super class is called Multiple Inheritance.in this case,there will be more than one super class and there can be one or more sub classes.
Note : In Java,only single inheritance is possible or you can say available.because multiple inheritance leads to confusion for the programmer.for example, class A has got a member x and class B has also got a member x. when another class C extends both the classes ,then there is a confusion regarding which copy of x is available in class C.





No comments: