Thursday , November 7 2024

Inheritance in Java

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.

Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.

The superclass and subclass have “is-a” relationship between them. Let’s have a look at the syntex below.

class Subclass-name extends Superclass-name 
{ 
//methods and fields 
}

The extends keyword indicates that you are making a new class that derives from an existing class.

In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.

Here is the complete example:

// A class to display the attributes of the vehicle
class Vehicle {
String color;
int speed;
int size;
void attributes() {
System.out.println("Color : " + color);
System.out.println("Speed : " + speed);
System.out.println("Size : " + size);
}
}

// A subclass which extends for vehicle
class Car extends Vehicle {
int CC;
int gears;
void attributescar() {
// The subclass refers to the members of the superclass
System.out.println("Color of Car : " + color);
System.out.println("Speed of Car : " + speed);
System.out.println("Size of Car : " + size);
System.out.println("CC of Car : " + CC);
System.out.println("No of gears of Car : " + gears);
}
}
public class Test {
public static void main(String args[]) {
Car b1 = new Car();
b1.color = "Blue";
b1.speed = 200 ;
b1.size = 22;
b1.CC = 1000;
b1.gears = 5;
b1.attributescar();
}
}
The output is

Color of Car : Blue
Speed of Car : 200
Size of Car : 22
CC of Car : 1000
No of gears of Car : 5

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.

 

typesofinheritance

Constructors and Inheritance

The constructor in the superclass is responsible for building the object of the superclass and the constructor of the subclass builds the object of subclass. When the subclass constructor is called during object creation, it by default invokes the default constructor of super-class. Hence, in inheritance the objects are constructed top-down. The super class constructor can be called explicitly using the keyword super, but it should be first statement in a constructor. The keyword super always refers to the superclass immediately above of the calling class in the hierarchy. The use of multiple super keywords to access an ancestor class other than the direct parent is illegal.

class Shape {
private int length;
private int breadth;
public int getBreadth() {
return breadth;
}
public int getLength() {
return length;
}
public void setBreadth(int i) {
breadth = i;
}
public void setLength(int i) {
length = i;
}
// default Constructor
Shape() {
length = 0;
breadth = 0;
System.out.println("Inside default constructor of Shape ");
}

// Parameterized Constructor
Shape(int len, int bdth) {
length = len;
breadth = bdth;
System.out.println("Inside constructor of Shape ");
System.out.println("length : " + length);
System.out.println("breadth : " + breadth);
}
}

// A subclass which extends for shape
class Rectangle extends Shape {
private String type;

// default Constructor
Rectangle() {
super();
type = null;
System.out.println("Inside default constructor of rectangle ");
}

// Parameterized Constructor
Rectangle(String ty, int len, int bdth) {
super (len, bdth);
System.out.println("Inside constructor of rectangle ");
System.out.println("length : " + len);
System.out.println("breadth : " + bdth);
System.out.println("type : " + type);
}

public String getType() {
return type;
}

public void setType(String string) {
type = string;
}
}

// A subclass which extends for rectangle
class ColoredRectangle extends Rectangle {
private String color;
/* default Constructor*/
ColoredRectangle() {
super();
color = null;
System.out.println("Inside default constructor of coloredRectangle");
}

// Parameterized Constructor
ColoredRectangle(String c, String ty, int len, int bdth) {
super (ty, len, bdth);
System.out.println("Inside constructor of coloredRectangle ");
System.out.println("length : " + len);
System.out.println("breadth : " + bdth);
System.out.println("type : " + ty);
}
public String getColor() {
return color;
}
public void setColor(String string) {
color = string;
}
}

public class Test {
public static void main(String args[]) {
ColoredRectangle CR = new ColoredRectangle();
ColoredRectangle CR2 = new ColoredRectangle("Red","Big", 5, 2 );
}
}

The output is:

Inside default constructor of Shape
Inside default constructor of rectangle
Inside default constructor of coloredRectangle
Inside constructor of Shape
length : 5
breadth : 2
Inside constructor of rectangle
length : 5
breadth : 2
type : null
Inside constructor of coloredRectangle
length : 5
breadth : 2
type : Big

 

 

About admin

Check Also

Arrays in java

The array is a data structure which stores a fixed-size sequential collection of elements of …

Leave a Reply