Module-3
Module-3
• Syntax :
• // Driver Class
• class Inherit {
• public static void main(String args[])
• {
• Engineer E1 = new Engineer();
• System.out.println("Salary : " + E1.salary + "\nBenefits : " + E1.benefits);
• }
•
Superclass Variable Reference Subclass Object
Key Points:
In Java, a superclass variable can hold a reference to a subclass object.
This is an example of polymorphism.
Allows using a superclass type to reference subclass objects dynamically.
Example Code:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Animal myDog = new Dog(); // Superclass reference to subclass object
myDog.sound(); // Outputs: Dog barks
Using super
Key Points:
The super keyword is used to call the superclass's methods and constructors.
super can be used to refer to the superclass's hidden fields and methods.
Helpful in method overriding when the subclass needs to call the superclass version.
Example Code:
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "black";
void printColor() {
System.out.println(color); // prints color of Dog class
System.out.println(super.color); // prints color of Animal class
}
}
Dog d = new Dog();
d.printColor();
Method Overriding
Key Points:
Method overriding allows a subclass to provide a specific implementation of a
method already defined in its superclass.
The method in the subclass should have the same name, return type, and
parameters.
Used for runtime polymorphism and to implement specific behavior in a
subclass.
Example Code:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
Animal myCat = new Cat();
myCat.sound(); // Outputs: Cat meows
Using Abstract Classes
Key Points:
Abstract classes cannot be instantiated directly.
They can have abstract methods (without a body) and concrete methods (with
implementation).
Subclasses must implement all abstract methods unless the subclass is also
abstract.
Example Code:
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal is sleeping");
}
}
class Lion extends Animal {
void sound() {
System.out.println("Lion roars");
}
}
Lion lion = new Lion();
lion.sound(); // Outputs: Lion roars
lion.sleep(); // Outputs: Animal is sleeping
Final with Inheritance
Key Points:
The final keyword can be used to prevent inheritance of a class.
A final method cannot be overridden by subclasses.
Useful for classes and methods that are critical for security or
intended to remain unchanged.
Example Code:
final class FinalClass {
final void finalMethod() {
System.out.println("This is a final method.");
}
}
// The following code will cause a compile-time error
// class SubClass extends FinalClass {} // Error: Cannot inherit
from final class
Abstract Classes and Dynamic Method
Key Points: Dispatch
Dynamic method dispatch is the mechanism by which a call to an overridden
method is resolved at runtime.
An abstract class can be used to define common behavior, and subclasses can
provide specific implementations.
Useful for designing systems where the exact type of objects might not be known
until runtime.
Example Code:
abstract class Animal { abstract void sound(); }
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}}
Animal myAnimal; myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dog barks
myAnimal = new Cat();
myAnimal.sound(); // Outputs: Cat meows
Summary
Key Takeaways:
A superclass reference can point to a subclass object, enabling
polymorphism.
The super keyword allows access to superclass members and constructors.
Method overriding enables subclasses to provide specific implementations
of superclass methods.
Abstract classes provide a framework for subclasses, ensuring they
implement specific methods.
Final keyword prevents further inheritance and method overriding.
Dynamic method dispatch facilitates runtime method resolution based on
object type.
Questions & Answers
Thank You!