Single Inheritance in Java
In Java, single inheritance is the ability of a class to inherit properties and methods from only one parent class. This means that a child class can have only one direct parent class.
To create a single inheritance relationship, we use the extends
keyword followed by the name of the parent class in the declaration of the child class. For example:
javaclass Parent {
// properties and methods
}
class Child extends Parent {
// additional properties and methods
}
In this example, the Child
class extends the Parent
class, which means that the Child
class inherits all the properties and methods of the Parent
class. The Child
class can also have its own additional properties and methods that are not present in the Parent
class.
It's important to note that Java does not support multiple inheritance, which means that a class cannot extend more than one parent class at a time. However, a class can implement multiple interfaces, which can provide similar functionality as multiple inheritance.
Comments
Post a Comment