Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday, 24 July 2017

Inheritance Programs In Java

Java Inheritance

Inheritance Programs in Java

Java inheritance means re-usability. In java inheritance, child class inherits parent class so that child class can use the data member and member function of its parent class.

Child class can reuse the properties and behavior of parent class and child class can also add a personal methods.

In java Basically java inheritance represents Is-a relationship i.e parent child relationship.

Java inheritance basically used to achieve method overriding or dynamic binding in java and it is also used for code re-usability.

There are 5 types of inheritance in java, These are

  1. Single level inheritance.
  2. Multilevel inheritance.
  3. Hierarchical inheritance.
  4. Multiple inheritance.
  5. Hybrid inheritance.

In java there are five types of inheritance but through a class there are 3 types of inheritance is supported which is single level, multilevel and hierarchical inheritance.

Java doesn't support multiple inheritance through class but multiple inheritance can be achieved through interface in java.

So here we will discuss only 3 types of inheritance which can be achieved through class.

So let's starts.

(1) Single Level Inheritance

In single level inheritance, When a class inherit another class class is known as single level inheritance e.g class B extends class A.

Java Single Level Inheritance Example

class Vehicle
{
void speed()
{
System.out.println("Vehicle's average speed is 40");
}
}
class Honda extends Vehicle
{
void run()
{
System.out.println("Honda run fast");
}
public static void main(String args[])
{
Honda h = new Honda();//creating Honda's object
h.speed();//calling Vehicle's method 
h.run();
}
}

output : Vehicle's average speed is 40
              Honda run fast

In the above example, Honda class inherit the Vehicle class and by using object of Honda class we can access the methods of Vehicle class because Honda class inherit the Vehicle class.


(2) Multilevel Inheritance

Java multilevel inheritance means when class C extends class b and class B extends class A i.e class C extends B extends A.

Java Multilevel Inheritance Example

class Principal
{
void manageTeachers()
{
System.out.println("Principal manage teachers");
}
}
class Teacher extends Principal 
{
void manageStudents()
{
System.out.println("Teacher manage students");
}
}
class Student extends Teacher
{
void read()
{
System.out.println("Students reads book");
}
public static void main(String args[])
{
Student s = new Student();
s.manageTeachers();
s.manageStudents();
s.read();
}
}

output : Principal manage teachers
              Teacher manage students
               Students reads book


(3) Java Hierarchical Inheritance

In java hierarchical inheritance 2 different class extends same class i.e class C extends class A and class B extends class A.

Java Hierarchical Inheritance Example

class Principal
{
void manageAll()
{
System.out.println("Principal manage all");
}
}
class Teacher extends Principal 
{
void manageStudents()
{
System.out.println("Teacher manage students");
}
}
class Student extends Principal
{
void read()
{
System.out.println("Students reads book");
}
public static void main(String args[])
{
Student s = new Student();
s.manageAll();
s.manageStudents();
s.read();
}
}

output :  Principal manage all
               Teacher manage students
               Students reads book

Q. Can we achieve multiple inheritance through class in java ?

Ans. No, we cannot achieve multiple inheritance through class in java but it is possible through interface.

Q. Can we inherit final class in java ?

Ans. No, we cannot inherit final class in java.

Share:

1 comment:

  1. Good work sir, Thanks for the proper explanation about Inheritance . I found one of the good resource related JAVA and OOPS concepts. It is providing in-depth knowledge on JAVA and OOPS. which I am sharing a link with you where you can get more clear on JAVA and OOPS. To know more Just have a look at this link

    Java Tutorial
    Class and object
    Inheritance
    Polymorphism
    Abstraction
    Encapsulation

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate