2:11 pm - Friday September 22, 7854

Inheritance in java with Example Program

Inheritance in java

What is inheritance?

Inheritance is the process of occurring properties of one class to another, It increases code reusability by grouping common logics and data’s in a class and allows other classes to inherit it without rewriting it.

class Vehicle

{

String brand;

String color;

}
class Car extends Vehicle

{

int totalDoor;

}
class Bike extends Vehicle

{

}

In the above example

Vehicle is the super class or base class that holds the common property of Car and Bike.

Car and Bike is the sub class or derived class that inherits the property  of class Vehicle

extends is the keyword used to inherit a class

 

Inheritance not supported by Java

Multiple inheritance(a class cannot has more than one super class) is not supported in java

Example of multiple inheritance(not supported in java)


class Vehicle

{

}

class Car

{

}

class Truck extends Vehicle, Car

{

}

In the above example class Truck tries to occur the property of both Vehicle and Car Which makes the code to fail at compilation in java.

 

If we need the both the property of Vehicle and Car in Truck class we can redesign the code as


class Vehicle

{

}

class Car extends Vehicle

{

// occurs the property of class Vehicle

}

class Truck extends Car

{

// occurs the property of both car and vehicle

}

 

Inheritance Example


class Vehicle

{

String brand;

String color;

}

class Car extends Vehicle

{

int totalDoor;

}

class Bike extends Vehicle

{

int cc;

}
class Main

{

public static void main(String args[])

{

Car c1 = new Car();

// accessing property from class Vehicle

c1.brand = "Honda";

c1.color = "black";

// accessing property from class Car

c1.totalDoor = 4;

Bike b1 = new Bike();

// accessing property from class Vehicle

b1.brand = "TVS";

b1.color = "blue";

// accessing property from class Bike

b1.cc = 150;

System.out.println("Car data " + c1.brand + " " + c1.color + " "
+ c1.totalDoor);

System.out.println("Bike data " + b1.brand + " " + b1.color + " "
+ b1.cc);

}

}

Default super class in Java

Every class in java by defaults extends the class Object from java.lang package

Ex:


class A

{

}

class B extends A

{

}

Here B is the subclass of A, and A is the subclass of Object.

In java, Object is the only class that doesn’t have any super class.

 

Inheritance Advanced tutorial (not recommended for beginners )

 

Inheritance with constructor and super()

Constructor cannot be inherited, Only the member of the class and methods can be inherited.

But, subclass constructor can access the super class constructor by using super() in its first line.


class A

{

A()

{

}

}

class B extends A{

B()

{

super();

}

}

 Inheritance with static variables

There is no need for inheriting static variable, static variable are shared values they can be accessed in any class by specifying its class name. A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. In the below example

Example


class A {

static int a = 10;

}

class B extends A {

}

public class StaticVariableInheritance

{

public static void main(String[] args) {

System.out.println(B.a);

System.out.println(A.a);

}

}

 

The above code compiles fine and produces the output

10

10

 Inheritance with static methods

Same as variable there is no need for inheriting the static method, simply static method cannot be inherited.

Inheritance with private variables & methods

Since private variable and methods are accessed only within the class, they cannot be inherited

Inheritance with abstract class & methods

When a class extends the abstract class all its abstract methods must be implemented in the subclass unless the subclass is also abstract. Abstract class are made to be inherited.

Inheritance with final variable and methods

Final variable and method can be inherited but final method cannot be changed or overridden in its subclass.

 

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply

Social Widgets powered by AB-WebLog.com.