Inheritance In java
The process by which one class acquires the properties and functionalities of another class is called inheritance. It provides code reusability.
Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.
The parent class is called the base class or super class. The child class that extends the base class is called the derived class or sub class or child class.
- Inheritance is the process of creating a new class by extending the common data members and methods of an existing class.
- Inheritance allows us to reuse code, which increases java application reusability.
- The base class or super class is the parent class. The derived class, subclass, or child class is a child class that extends the base class.
- The biggest advantage of Inheritance is that the code in base class need not be rewritten in the child class
Syntax :
To inherit a class we use extends keyword. Here class A is child class and class B is parent class.
class A extends B
{
}
there are four type of inheritance in java;
1.Single Inheritance:
refers to a child-parent class relationship in which one class extends the another class.
2. Multilevel Inheritance :
refers to a child and parent class relationship where a class extends the child class.
3. Hierarchical Inheritance :
refers to a child and parent class relationship where more than one classes extends the same class.
4. Multiple Inheritance :
refers to the concept of one class extending more than one classes, which means a child class has two parent classes. Java doesn’t support multiple inheritance.
4. Hybrid Inheritance :
like multiple inheritance java does not support Hybrid inheritance. To reduce the complexity and simplify the language, multiple/Hybrid inheritance is not supported in java
Let us know briefly about each type :
1.Single Inheritance:
When a class inherits another class, it is known as a single inheritance. In the example given below, Programmer class inherits the Emp class, so there is the single inheritance.
Example:
//Programmer.java
class Emp
{
String designation = "Developer";
String company = "company1";
void does()
{
System.out.println("App Development");
}
}
public class Programmer extends Emp
{
String mainLang = "Java";
public static void main(String args[])
{
Programmer obj = new Programmer();
System.out.println(obj.company);
System.out.println(obj.designation);
System.out.println(obj.mainLang);
obj.does();
}
}
Output :
company1
Developer
Java
App Development
2.Multilevel Inheritance:
refers to a child and parent class relationship where a class extends the child class. For example class A extends class B and class B extends class C. there is a chain of inheritance is known as multilevel inheritance.
Example:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark() {
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep() {
System.out.println("weeping...");
}
}
class Testmain
{
public static void main(String args[])
{
BabyDog d = new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output :
weeping...
barking...
eating...
3.Hierachical Inheritance:
refers to a child and parent class relationship where more than one classes extends the same class.
Example:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class Testmain
{
public static void main(String args[])
{
Cat c = new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
Output :
meowing...
eating...
4.Multiple Inheritance:
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
For example: Class C extends class A and B then this type of inheritance is known as multiple inheritance. Java doesn’t allow multiple inheritance.
Why Java doesn't support multiple inheritance ?
Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. we can use interfaces instead of classes to achieve the same purpose.
Example :class A
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A, B
{ //suppose if it were
public static void main(String args[])
{
C obj = new C();
obj.msg(); //Now which msg() method would be invoked?
}
}
Output :
Compile Time Error
Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it.
we can use interfaces instead of classes to achieve the same purpose.
class A
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A, B
{ //suppose if it were
public static void main(String args[])
{
C obj = new C();
obj.msg(); //Now which msg() method would be invoked?
}
}
Output :
Compile Time Error
5.Hybrid Inheritance:
like multiple inheritance java does not support Hybrid inheritance. To reduce the complexity and simplify the language, multiple/Hybrid inheritance is not supported in java.
What is Diamond Problem in java?
Diamond problem in java |
Class D extends both classes B & C. Now lets assume we have a method in class A and class B & C overrides that method in their own way. Wait!! here the problem comes – Because D is extending both B & C so if D wants to use the same method which method would be called (the overridden method of B or the overridden method of C). Ambiguity. That’s the main reason why Java doesn’t support multiple inheritance.
we can use interfaces instead of classes to achieve the same purpose.
we implement more than one interfaces in a class
Example:
interface A
{
public void myMethod();
}
interface B
{
public void myMethod();
}
class Testjava implements A, B
{
public void myMethod()
{
System.out.println("Implementing interfaces");
}
public static void main(String args[])
{
Testjava obj = new Testjava();
obj.myMethod();
}
}
Output :
Implementing interfaces
Tags:
Java