0% found this document useful (0 votes)
6 views2 pages

Java Inheritance Test Paper

The document is a test paper on Java inheritance, structured in five phases covering inheritance basics, the use of 'super', method overriding, dynamic method dispatch, and medium challenges. Each phase includes practical coding tasks that illustrate different inheritance types, method overriding, and the implications of final classes and methods. The tasks encourage understanding of Java's inheritance model through class creation, method implementation, and error handling.

Uploaded by

parnikadesk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Inheritance Test Paper

The document is a test paper on Java inheritance, structured in five phases covering inheritance basics, the use of 'super', method overriding, dynamic method dispatch, and medium challenges. Each phase includes practical coding tasks that illustrate different inheritance types, method overriding, and the implications of final classes and methods. The tasks encourage understanding of Java's inheritance model through class creation, method implementation, and error handling.

Uploaded by

parnikadesk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA INHERITANCE TEST PAPER.

PHASE 1: INHERITANCE BASICS & TYPES

1. The Royal Bloodline (Single Inheritance) Create a class GrandKing with a variable houseName. Create
a subclass Prince. Initialize the houseName in the GrandKing class. Show that the Prince class can
print the houseName without declaring it again.

2. The Tech Tree (Multilevel Inheritance) Create a class Device. Create a subclass Smartphone that
extends Device. Create another subclass IPhone that extends Smartphone. Add one unique method
in each class. Create an object of IPhone and call all three methods.

3. The Zoo Directory (Hierarchical Inheritance) Create a base class Animal with a variable name. Create
two subclasses Lion and Elephant. Show that both subclasses can access the name variable.
Demonstrate that Lion and Elephant are independent of each other.

4. The Hybrid Limitation Create two classes Phone and Clock. Attempt to create a class SmartWatch
that extends both Phone and Clock. Observe the compiler error. Write a comment explaining why
Java does not support multiple inheritance using classes.

PHASE 2: THE POWER OF super

1. The Pizza Order Create a class Pizza with a constructor that accepts an integer size. Create a subclass
SpecialtyPizza with a constructor that accepts size and a topping. Use super(size) to pass the size to
the parent class.

2. The Secret Agent Create a class Agent with a method mission(). Create a subclass Spy that overrides
the mission() method. Use [Link]() inside the overridden method to print the base mission
first.

3. The Bank Vault Create a class Account with a private variable balance. Initialize the balance using a
constructor. Create a subclass SavingsAccount. Show how the subclass can set the balance using
super() even though it cannot access the variable directly.

PHASE 3: METHOD OVERRIDING & final

1. The Video Game Character Create a class Hero with a method attack(). Create a subclass Mage.
Override the attack() method to display a different behavior.

2. The Unbreakable Contract (Final Method) Create a class Employer with a final method paySalary().
Create a subclass Manager. Attempt to override the paySalary() method. Observe and explain the
compiler error.

1
3. The Dead End (Final Class) Mark a class ConfidentialDocument as final. Attempt to create a subclass
LeakedDocument. Explain why inheritance is not allowed in this case.

PHASE 4: DYNAMIC METHOD DISPATCH

1. The Shape Shifter Create a class Shape with a method draw(). Create subclasses Circle and Square
that override draw(). Use a Shape reference to point to different objects and call draw().

2. The Universal Remote Create a base class ElectronicDevice with a method powerOn(). Create
subclasses TV, Radio, and AC. Store their objects in an ElectronicDevice array. Loop through the array
and call powerOn() on each object.

PHASE 5: MEDIUM CHALLENGES

1. The RPG Leveling System Create a class Fighter with a method calculateDamage(int strength). Create
a subclass Berserker that overrides the method. Use a parent class reference to call the method.
Demonstrate runtime polymorphism.

2. The Smart Warehouse Create a class Item with a final variable TAX_RATE initialized to 0.15. Create a
subclass LuxuryItem. Attempt to modify TAX_RATE in the subclass constructor. Observe and explain
the result.

3. The Ecosystem Simulation Create a class Organism with a method breathe(). Create a subclass Fish
that overrides breathe(). Create a method checkLife(Organism o) that calls [Link](). Pass a Fish
object to demonstrate upcasting and dynamic method dispatch.

You might also like