Inheritance in java

Share:
What is Inheritance in Java?
Inheritance in Java is a technique/mechanism in which child class object can acquires all the properties and behavior of parent class object.It is an important part of OOP's concept in Java.

The purpose behind inheritance concept in Java is that we can create a new classes that are built upon existing classes.When we inherit(extends) from an existing class,we can reuse methods and fields of the parent class.

Note:We can add new methods and fields in our current class,we can develop our current class according to our requirement,no restrictions.

In JAVA Inheritance represents the IS-A relationship concept which is also known as  parent-child relationship.

Why Inheritance in Java
  • If you want to override a method then Inheritance is the first requirement.
  • Code Reusabiltiy.
Components of Inheritance
Class
A class is a blueprint from objects are created.

Child Class/Sub Class
Sub OR Child class is a class which inherits/extends the other class.It is also known as derived class.

Parent/Super Class
Super OR Parent class is a class from where a child class inherits the features.It is also known as Base Class.

Reusability
As the name says, reusabilty is a technique which allow us to reuse the fields and methods of an existing class when we creates a new classes. We can use the same fields and methods that is already defined in the parent class.

Standard Syntax of Java Inheritance

class Child/Sub_Class-Name extends Parent/Super_Class-Name
{
//methods and fields Depend on requirement
}



The extends keyword ensures that we are creating a new class that derives from an existing class.The meaning of  extends keyword is to increase the functionality.
In other words a class which is inherited is known as Parent/Base Class,and a new class which is inheriting the properties of Base/Parent Class known as child class.

Java Inheritance Example
class Sport{
String sportName="CRICKET";
}
public class Mumbai extends Sport{
String msg="Mumbaikar's Best Sport is ";

 public static void main(String args[]){
    Mumbai m=new Mumbai();
    System.out.println(m.msg+m.sportName);
   }
}
output:
Mumbaikar's Best Sport is CRICKET
Note: In the above example Mumbai Class object can access it's own field i.e. msg as well as other class field i.e. sportName of  Sport class i.e., Code Reusability.

Types of inheritance in Java


Note: Multiple Inheritance not supported in Java through Class.When one class inherits multiple classes known as Multiple inheritance.





Single Inheritance Example
File Name: TestMain.java
class Sport{
String sportName="CRICKET";
}
class Mumbai extends Sport{
String msg1="Mumbaikar's Best Sport is ";
}
public class TestMain{
    public static void main(String args[]){
    Mumbai m=new Mumbai();
    System.out.println(m.msg1+m.sportName);
   }
}
output:
Mumbaikar's Best Sport is CRICKET

Multilevel Inheritance Example
File Name: TestMain2.java
class Sport{
String sportName="CRICKET";
}
class Mumbai extends Sport{
String msg1="Mumbaikar's Best Sport is ";
}
class Delhi extends Mumbai{
 String msg2="Best sport of Delhi is ";
}
public class TestMain2{
   public static void main(String args[]){
    Delhi d=new Delhi();
    System.out.println(d.msg2+d.sportName);
   }
}
output:
Best sport of Delhi is CRICKET

Hierarchical Inheritance Example
File Name: TestMain3.java
class Sport{
String sportName="CRICKET";
}
class Mumbai extends Sport{
String msg1="Mumbaikar's Best Sport is ";
}
class Delhi extends Sport{
 String msg2="Best sport of Delhi is ";
}
public class TestMain3{
  public static void main(String args[]){
  Delhi d=new Delhi();
  System.out.println(d.msg2+d.sportName);
  //but now below trying to access msg1 of Mumbai Class
  //System.out.println(d.msg1);//compile time error:msg1 can not be resolved or is not a field
   }
}
output:
Best sport of Delhi is CRICKET

Multiple Inheritance Example
Multiple Inheritance not supported in Java!Why?

-To simplify and to reduce the complexity Multiple Inheritance not supported in Java.
-Consider the below example,we have three classes i.e., Mumbai,Delhi and TestMain4 with main method. And Mumbai and Delhi both have same msg() method.Here TestMain4 extends Mumbai and Delhi,  and if we try to call msg() method using the object of child class i.e. TestMain4 then there will be ambiguity to call the msg() method of Mumbai OR Delhi.

Since compile time error is better than run time error. So whether we have same or different method there will be compile time error if you try to extending two classes.see below example...

File Name: TestMain4.java

class Mumbai  {
   void msg(){
     System.out.println("Playing Cricket");
   }
}

class Delhi  {
   void msg(){
   System.out.println("Playing Cricket");
  }
}

public class TestMain4 extends Mumbai,Delhi  {
   public static void main(String args[]){
    TestMain4 obj=new TestMain4();
    obj.msg();//Now here which msg() method would be invoked
   }
}
output :compile time error


Hope This Article Was Helpful.

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database