Java Module 4 Answers
Java Module 4 Answers
BPLCK105C
MODULE 4
1. Single Inheritance
A subclass inherits from a single parent class.
class Parent {
void show() { System.out.println("This is Parent class"); }
}
2. Multilevel Inheritance
A class inherits from another class, which is itself inherited from another.
class Grandparent {
void show() { System.out.println("Grandparent class"); }
}
3. Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
class Parent {
void show() { System.out.println("Parent class"); }
}
4. Hybrid Inheritance
A combination of multiple types of inheritance using interfaces.
interface A {
void show();
}
interface B {
void display();
}
class C implements A, B {
public void show() { System.out.println("Implementing A"); }
public void display() { System.out.println("Implementing B"); }
}
Key Points:
✔ Java does not support multiple inheritance with classes to avoid ambiguity.
✔ Hybrid inheritance is achieved using interfaces.
✔ Private members are not inherited by subclasses.
Definition:
The super keyword is used to access parent class methods,
constructors, and variables inside a subclass.
Syntax:
class Parent {
int a = 10;
void show() { System.out.println("Parent method"); }
}
Example :
class Parent {
Parent() { System.out.println("Parent Constructor"); }
}
Key Points:
✔ super() must be the first statement in a constructor.
✔ Used to access parent class members when they are hidden
by the child class.
Definition:
A superclass variable can reference a subclass object, but it can
only access members of the superclass.
Syntax:
Parent obj = new Child(); // Superclass reference to subclass
object
Example :
class Parent {
int x = 10;
}
Key Points:
✔ Can be used for runtime polymorphism.
✔ Cannot access subclass-specific members unless typecasting
is done.
4.Method Overriding
Definition:
Method overriding occurs when a subclass provides a specific implementation
of a method that is already defined in its parent class.
Syntax:
class Parent {
void show() { System.out.println("Parent Method"); }
}
Example :
class Parent {
void show() { System.out.println("Parent Method"); }
}
Key Points:
✔ The method name and parameters must be the same.
✔ Cannot override final methods.
Definition:
Final Class: A class that cannot be inherited.
Final Method: A method that cannot be overridden.
Syntax:
final class Parent {
final void show() { System.out.println("Final
Method"); }
}
Example :
final class Parent {
void show() { System.out.println("Parent Method"); }
}
// class Child extends Parent {} // ERROR: Cannot extend final class
Key Points:
✔ final prevents modification of class or method.
✔ Helps in performance optimization.
Definition:
Dynamic method dispatch (runtime polymorphism) allows
method calls to be resolved at runtime based on the actual
object type.
Syntax:
Parent obj = new Child(); // Runtime polymorphism
obj.method();
Example :
class Parent {
void display() { System.out.println("Parent Display"); }
}
Key Points:
✔ Java determines method execution at runtime.
✔ Used in achieving polymorphism.
7.Abstract Class
Definition:
An abstract class cannot be instantiated and may have abstract methods that
must be implemented by subclasses.
Syntax:
Example :
abstract class Animal {
abstract void makeSound();
}
Key Points:
✔ Used when we want a base class but don't want direct object
creation.
✔ Must be extended and its abstract methods must be
implemented.