Is a Java feature that can acquire all the properties and Behaviours of the parent object?

Inheritance is an object-oriented programming (OOP) concept by which the properties and behaviors from a parent class are passed on to a child class.

In day-to-day life, when something gets passed from a parent to a child, it is said that child inherits from their parent. Similarly, in Java, when some properties or behaviors get passed from parent class to the child class, it is said that child class inherits from a parent class.

Child classes can have properties and methods of its own as well.

They can also override the behaviors (methods) of the parent class. This is called polymorphism (another OOP concept).

We can implement inheritance by using the extends keyword.

Syntax

Here’s the basic syntax for implementing inheritance:

public class Child extends Parent {

}

Example

public class Vehicle {

private int numWheels;

private String engineNum;

private String maker;

private String color;

public int getNumWheels() {

return numWheels;

}

public void setNumWheels(int wheels) {

this.numWheels = wheels;

}

public String getEngineNum() {

return engineNum;

}

public void setEngineNum(String engineNum) {

this.engineNum = engineNum;

}

}

In the code above, Vehicle is a class that has properties and behaviors. This is more of a generic class.

While creating a specific class, for example, Car which has these properties and behaviors and some extra properties, instead of defining all these properties again in the new class, we can make use of inheritance to pass on the properties defined in Vehicle class and access them in Car class. This would make code written in Vehicle class reusable.

Similarly, we can make another class, for example, Airplane, which can access the properties present in Vehicle class.

Here’s a Car class which inherits Vehicle class:

public class Car extends Vehicle {

private int airbagCount;

public int getAirbagCount() {

return airbagCount;

}

public void setAirbagCount(int airbagCount) {

this.airbagCount = airbagCount;

}

}

Below is how you can access properties and methods of parent class:

public static void main(String args[]) {

Car car = new Car();

car.setNumWheels(4);

System.out.println(car.getNumWheels());

car.setAirbagCount(2);

System.out.println(car.getAirbagCount());

}

Types of Inheritance

Single Inheritance

Single inheritance is when a child class inherits from a parent class.

public class Parent {

}

public class Child extends Parent {

}

Multilevel Inheritance

Multilevel inheritance is when a child class inherits from a parent class which in turn inherits from another parent class.

public class Parent {

}

public class Child extends Parent {

}

public class GrandChild extends Child {

}

Hierarchical Inheritance

Hierarchical inheritance is when a child class inherits from a parent class and there is another sibling class that also inherits from the same parent class.

public class Parent {

}

public class Child extends Parent{

}

public class Sibling extends Parent{

}

Advantages of Inheritance

  • Code reusability: Same properties and methods of a class can be used by inheriting that class.
  • Lower maintenance cost: If a piece of code needs to be updated, it can be done at minimal place if inheritance is used.
  • Easier to add new features: If a same new feature needs to be added to multiple classes, through inheritance it can be added in parent class and all the child classes inheriting this parent class would get that feature instantly.

In which Java features one object can acquire all the properties and Behaviour of the parent object?

Inheritance. When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

In which features of Java object of one class acquires the properties and behavior of object of another class?

Inheritance is when an object acquires the property of another object. Inheritance allows a class (subclass) to acquire the properties and behavior of another class (super-class).

What is properties and Behaviour in Java?

We write codes that create Java objects from Java classes. These can have properties and behaviors. Properties are data with names; while, behaviors are the actions an object can perform on its properties.

What is the behavior of an object in Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.