Inheritance in Java

Inheritance basically in object oriented programming language means a class can acquire some properties of another class.

Base class or Parent Class:

The parent class that originally has a set of properties (methods).

Derived class or Child Class:

The child class that can inherit the methods from the parent class.

The keyword “extends” is used to inherit a base class. as for example:

class A extends class B

In the example given below the parent class is the class Person and the Men class is the child class which inherits the properties of parent class. Both the classes have name and surname but the child class also has additional information about height as well.


class Person {
 private String name, surname;
 public Person(String name, String surname){
 this.name=name;
 this.surname=surname;
 }
 public String getName(){
 return name;
 }
 public String getSurName(){
 return surname;
 }

}

class Men extends Person{
 private double height;
 public Men(String name, String surname, double height){
 super(name, surname);
 this.height=height;
 }
 public double getHeight(){
 return height;
 }
}

public class Inheritance{
 public static void main(String[] args){
 Men m = new Men("Paul", "Kezh", 2.5);//object of child class
 Person p= new Person("Ram", "Maho"); //object of parent class
 System.out.println("Man name: "+m.getName()+" Man Surname: "+m.getSurName()+" Man Height: "+m.getHeight());
 System.out.println("Person name: "+p.getName()+" Person Surname: "+p.getSurName());

 }
}

Output:

Man name: Paul Man Surname: Kezh Man Height: 2.5
Person name: Ram Person Surname: Maho

As further another class also extend the child class but the parent class which is extended once cannot be extended twice. Like in the example given below, we can create another class extending Men class but not Person class:


class Person {
 private String name, surname;
 public Person(String name, String surname){
 this.name=name;
 this.surname=surname;
 }
 public String getName(){
 return name;
 }
 public String getSurName(){
 return surname;
 }

}

class Men extends Person{
 private double height;
 public Men(String name, String surname, double height){
 super(name, surname);
 this.height=height;
 }
 public double getHeight(){
 return height;
 }
}

class YoungMen extends Men{
 private double age;
 public YoungMen(String name, String surname, double height, double age){
 super(name, surname, height);
 this.age=age;
 }
 public double getAge(){
 return age;
 }

}

public class Inheritance{
 public static void main(String[] args){
 Men m = new Men("Paul", "Kezh", 2.5);//object of child class
 Person p= new Person("Ram", "Maho"); //object of parent class
 YoungMen ym = new YoungMen("Pukar", "Shah", 5.6, 25);
 System.out.println("Man name: "+m.getName()+" Man Surname: "+m.getSurName()+" Man Height: "+m.getHeight());
 System.out.println("Person name: "+p.getName()+" Person Surname: "+p.getSurName());
 System.out.println("Young man name: "+ym.getName()+" Young man Surname: "+ym.getSurName()+" Young Man Height: "+ym.getHeight()+" Young Man Age: "+ym.getAge());

 }
}

Output:

Man name: Paul Man Surname: Kezh Man Height: 2.5
Person name: Ram Person Surname: Maho
Young man name: Pukar Young man Surname: Shah Young Man Height: 5.6 Young Man Age: 25.0

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s