Attachment (3)
Attachment (3)
Notes
Inheritance in Java :
java
Once the subclass is created, it can access all the public and protected methods
and fields of the superclass. The subclass can also override the methods of the
superclass if necessary. This allows the subclass to provide its own
implementation of the methods inherited from the superclass.
In summary, inheritance is a powerful feature in Java that allows for code reuse
and promotes the concept of code organization and hierarchy.
It's important to note that Java only supports single inheritance of classes, but it
supports multiple inheritance of interfaces.
java
// subclass implementation
In this example, the subclass "Car" inherits all the properties and methods of the
superclass "Vehicle", such as its attributes (e.g. color, model, year) and its
methods (e.g. accelerate, brake, turn). The subclass can also define its own
attributes and methods, which can be used in addition to those inherited from
the superclass.
Example -:
// Parent class
class Animal {
myDog.eat();
myDog.bark();
Multilevel Inheritance -:
class Animal {
System.out.println("Animal is eating");
System.out.println("Dog is barking");
In this example, the Dog class inherits from the Mammal class, which in turn
inherits from the Animal class. As a result, the Dog class has access to all the
properties and methods defined in both the Mammal and Animal classes.
For example:
// implementation
// implementation
It is important to note that if two interfaces declare a method with the same
name and signature, the implementing class only needs to provide one
implementation for both of them.