Welcome back to my blog! In this post, you will learn about Inheritance in Java.
Basically, Inheritance in Java is like a parent-child relationship. The child class acquires all the properties of the parent class.
You can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. You can also add new methods and fields.
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Suzuki extends Car{
public Suzuki()
{
System.out.println("Class Suzuki");
}
public void brand()
{
System.out.println("Brand: Suzuki");
}
public void speed()
{
System.out.println("Max: 100Kmph");
}
}
public class Suzuki-X extends Suzuki{
public Suzuki-X()
{
System.out.println("Suzuki Model: Z");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Suzuki-X obj=new Suzuki-X();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Class Car
Class Suzuki
Suzuki Model: X
Vehicle Type: Car
Brand: Suzuki
Max: 100Kmph
class HierarchicalInheritance {
void DisplayA()
{
System.out.println("This is a content of parent class");
}
}
//B.java
class A extends HierarchicalInheritance {
void DisplayB()
{
System.out.println("This is a content of child class 1");
}
}
//c.java
class B extends HierarchicalInheritance {
void DisplayC()
{
System.out.println("This is a content of child class 2");
}
}
//MainClass.java
class HierarchicalInheritanceMain {
public static void main(String args[])
{
System.out.println("Calling for child class C");
B b = new B();
b.DisplayA();
b.DisplayC();
System.out.println("Calling for child class B");
A a = new A();
a.DisplayA();
a.DisplayB();
}
}
Calling for child class C
This is a content of parent class
This is a content of child class 2
Calling for child class B
This is a content of parent class
This is a content of child class 1
public void disp()
{
System.out.println("C");
}
}
class A extends C
{
public void disp()
{
System.out.println("A");
}
}
class B extends C
{
public void disp()
{
System.out.println("B");
}
}
class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){
D obj = new D();
obj.disp();
}
}
D
Thank you for coming to my blog. If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
For more Java tutorials, visit my blog:
www.javaoptimist.blogspot.com
Basically, Inheritance in Java is like a parent-child relationship. The child class acquires all the properties of the parent class.
You can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. You can also add new methods and fields.
Why Inheritance is used:
- For method over-riding,
- To reuse code.
Syntax: class subclass-name extends superclass-name
{
//methods and fields
}
Here, the keyword "extends" is used to derive all the properties from the superclass, i.e, the parent class.
Types of Inheritance in Java:
1. Single Inheritance,
2. Multilevel Inheritance,
3. Hierarchical Inheritance,
4. Multiple Inheritance,
5. Hybrid Inheritance.
1. Single Inheritance:
In single inheritance, a child class extends its parent class.
2. Multilevel Inheritance:
In multilevel inheritance, a parent class has a child class and that child class has its own child class and so on. Each parent class is extended by its own child class.
3. Hierarchical Inheritance:
There are only one parent class and more than one child class. The children classes extend its parent class.
4. Multiple Inheritance:
One child class extends more than one parent class.
5. Hybrid Inheritance:
Note: Multiple Inheritance is not supported in Java.
Examples:
1. Simple Inheritance:
class Vehicle {
void run() {
System.out.println("running...");
}
}
class Car extends Vehicle {
void move() {
System.out.println("moving...");
}
}
class TestInheritance {
public static void main(String args[]) {
Car c=new Car();
c.move();
c.run();
}
}
Output:
moving...
running...
2. Multilevel Inheritance:
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Suzuki extends Car{
public Suzuki()
{
System.out.println("Class Suzuki");
}
public void brand()
{
System.out.println("Brand: Suzuki");
}
public void speed()
{
System.out.println("Max: 100Kmph");
}
}
public class Suzuki-X extends Suzuki{
public Suzuki-X()
{
System.out.println("Suzuki Model: Z");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Suzuki-X obj=new Suzuki-X();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Output:
Class Car
Class Suzuki
Suzuki Model: X
Vehicle Type: Car
Brand: Suzuki
Max: 100Kmph
3. Hierarchical Inheritance:
void DisplayA()
{
System.out.println("This is a content of parent class");
}
}
//B.java
class A extends HierarchicalInheritance {
void DisplayB()
{
System.out.println("This is a content of child class 1");
}
}
//c.java
class B extends HierarchicalInheritance {
void DisplayC()
{
System.out.println("This is a content of child class 2");
}
}
//MainClass.java
class HierarchicalInheritanceMain {
public static void main(String args[])
{
System.out.println("Calling for child class C");
B b = new B();
b.DisplayA();
b.DisplayC();
System.out.println("Calling for child class B");
A a = new A();
a.DisplayA();
a.DisplayB();
}
}
Output:
This is a content of parent class
This is a content of child class 2
Calling for child class B
This is a content of parent class
This is a content of child class 1
4. Hybrid Inheritance:
class C
{public void disp()
{
System.out.println("C");
}
}
class A extends C
{
public void disp()
{
System.out.println("A");
}
}
class B extends C
{
public void disp()
{
System.out.println("B");
}
}
class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){
D obj = new D();
obj.disp();
}
}
Output:
Thank you for coming to my blog. If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
For more Java tutorials, visit my blog:
www.javaoptimist.blogspot.com
Comments
Post a Comment