Inheritance is a mechanism in Java that allows a class to inherit properties and methods from another class. The class that inherits properties and methods is called a subclass or derived class, and the class from which it inherits is called the superclass or base class. Inheritance enables code reuse and helps to create a hierarchical structure of classes.
The syntax for inheritance in Java is as follows:
class Superclass {
// superclass fields and methods
}
class Subclass extends Superclass {
// subclass fields and methods
}
In this example, the Subclass is inheriting from the
Superclass. The Subclass can access all public and protected fields and methods of the
Superclass, as well as any public fields and methods inherited from the
Object class.
Subclasses can override methods inherited from their superclass using the
@Override annotation. They can also add their own fields and methods or define constructors. The
super keyword can be used to call the superclass constructor or to call a method from the superclass that has been overridden in the subclass.
Here's an example of inheritance in Java:
class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public void eat() {
System.out.println(name + " is eating dog food.");
}
public void bark() {
System.out.println(name + " is barking.");
}
}
In this example, Dog is a subclass of Animal. It inherits the
name field and eat() method from Animal, but it overrides
eat() to print a different message. It also adds a new method
bark().
Inheritance allows one class to derive properties and behavior from another class. The class that inherits properties is called a subclass or derived class, while the class that provides the properties is called the superclass or base class.
The subclass can inherit the following properties and behavior from its superclass:
The subclass can inherit instance variables and methods from its superclass. It can use these variables and methods as if they were defined within the subclass itself.
The subclass can inherit constructors from its superclass. It can use these constructors to create objects of the subclass.
The subclass can inherit access modifiers from its superclass. It can access public and protected members of the superclass, but not private members.
To inherit properties and behavior from a superclass, the subclass must use the "extends" keyword in its class definition. For example, the following code defines a subclass "Employee" that inherits properties and behavior from its superclass "Person":
public class Person {
protected String name;
public Person(String name) {
this.name = name;
}
public void printName() {
System.out.println("Name: " + this.name);
}
}
public class Employee extends Person {
private int employeeId;
public Employee(String name, int employeeId) {
super(name);
this.employeeId = employeeId;
}
public int getEmployeeId() {
return this.employeeId;
}
}
In this example, the subclass "Employee" extends the superclass "Person" using the "extends" keyword. The subclass inherits the "name" instance variable and "printName" method from the superclass, and it adds its own instance variable "employeeId" and method "getEmployeeId".
By using inheritance, we can reuse code and create hierarchies of related classes. It also helps in reducing code duplication and improving code maintainability.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Inheritance is a mechanism in Java that allows a class to inherit properties and methods from another class. The class that inherits properties and methods is called a subclass or derived class, and the class from which it inherits is called the superclass or base class. Inheritance enables code reuse and helps to create a hierarchical structure of classes.
The syntax for inheritance in Java is as follows:
In this example, the Subclass is inheriting from the Superclass. The Subclass can access all public and protected fields and methods of the Superclass, as well as any public fields and methods inherited from the Object class.
Subclasses can override methods inherited from their superclass using the @Override annotation. They can also add their own fields and methods or define constructors. The super keyword can be used to call the superclass constructor or to call a method from the superclass that has been overridden in the subclass.
Here's an example of inheritance in Java:
In this example, Dog is a subclass of Animal. It inherits the name field and eat() method from Animal, but it overrides eat() to print a different message. It also adds a new method bark().
Inheritance allows one class to derive properties and behavior from another class. The class that inherits properties is called a subclass or derived class, while the class that provides the properties is called the superclass or base class.
The subclass can inherit the following properties and behavior from its superclass:
To inherit properties and behavior from a superclass, the subclass must use the "extends" keyword in its class definition. For example, the following code defines a subclass "Employee" that inherits properties and behavior from its superclass "Person":
In this example, the subclass "Employee" extends the superclass "Person" using the "extends" keyword. The subclass inherits the "name" instance variable and "printName" method from the superclass, and it adds its own instance variable "employeeId" and method "getEmployeeId".
By using inheritance, we can reuse code and create hierarchies of related classes. It also helps in reducing code duplication and improving code maintainability.