Department of CSE, CUET Java OOP Lab Sheet
Lab Sheet: Object-Oriented Programming in Java –
Inheritance & Encapsulation
Course: Introduction to Programming Language II (Java)
Topic: Types of Inheritance, Encapsulation, super(), this(), Constructor Chaining
Level: Beginner to Intermediate
Duration: 2.5 hours
Prepared by: Ashraful Islam Paran, Dept. of CSE, SEU
1. Objectives
By the end of this lab, students will be able to:
• Implement Single, Multilevel, and Hierarchical inheritance.
• Understand why Java does not support Multiple Inheritance through classes.
• Use encapsulation with private fields and public methods.
• Use super() and this() for constructor chaining.
• Override methods and call parent methods using super.
2. What is Inheritance?
Inheritance allows a class (subclass) to inherit fields and methods from another class
(superclass) using the extends keyword. Benefits:
• Code Reusability
• Method Overriding
• Runtime Polymorphism
• Natural hierarchical organization
3. Types of Inheritance Supported in Java
• Single Inheritance (Class B extends Class A)
• Multilevel Inheritance (C extends B extends A)
• Hierarchical Inheritance (B, C, D extend A)
• Multiple & Hybrid → Not supported with classes (only via interfaces)
Prepared by Ashraful Islam Paran
Page 1
Department of CSE, CUET Java OOP Lab Sheet
4. Example 1: Single Inheritance
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // Inherited from Animal
[Link](); // Defined in Dog
}
}
Output:
This animal eats food.
The dog barks.
Task 4.1 – Single Inheritance Practice (10 min)
a. Create a class Bird that extends Animal.
b. Add a method fly() in Bird.
c. In main(), create a Bird object and call both eat() and fly().
d. Try creating an Animal reference that holds a Bird object (introduction to poly-
morphism).
Prepared by Ashraful Islam Paran
Page 2
Department of CSE, CUET Java OOP Lab Sheet
5. Example 2: Multilevel Inheritance
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
class Puppy extends Dog {
void weep() {
[Link]("The puppy weeps.");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
[Link](); // From Animal (Grandparent)
[Link](); // From Dog (Parent)
[Link](); // From Puppy
}
}
Output:
This animal eats food.
The dog barks.
The puppy weeps.
Task 5.1 – Multilevel Inheritance (12 min)
Create the chain: Vehicle → Car → SportsCar
• Vehicle has start() and stop()
• Car adds accelerate()
• SportsCar adds nitroBoost()
Demonstrate that a SportsCar object can call all four methods.
Prepared by Ashraful Islam Paran
Page 3
Department of CSE, CUET Java OOP Lab Sheet
6. Example 3: Hierarchical Inheritance
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
void bark() { [Link]("The dog barks."); }
}
class Cat extends Animal {
void meow() { [Link]("The cat meows."); }
}
class Puppy extends Dog {
void weep() { [Link]("The puppy weeps."); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
Puppy p = new Puppy();
[Link](); [Link]();
[Link](); [Link]();
[Link](); [Link](); [Link]();
}
}
Output:
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
This animal eats food.
The dog barks.
The puppy weeps.
Task 6.1 – Hierarchical Inheritance (15 min)
a. Create a base class Shape with method draw().
b. Create three subclasses: Circle, Rectangle, Triangle – each overriding draw()
with its own message.
c. In main(), create one object of each subclass and call draw().
Prepared by Ashraful Islam Paran
Page 4
Department of CSE, CUET Java OOP Lab Sheet
7. Example 4.1: Encapsulation – Student ID Card
class Student {
// Data is hidden (private)
private String name;
private int id;
// Constructor only way to set data
public Student(String name, int id) {
[Link] = name;
[Link] = id;
}
// Public methods only way to read data
public String getName() {
return name;
}
public int getId() {
return id;
}
// Nice display method
public void showInfo() {
[Link]("Name: " + name + " | ID: " + id);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Rahim", 2219001);
[Link]();
// These lines will NOT compile error (good!)
// [Link] = "Karim"; // private not allowed
// [Link]([Link]);
// Correct way use getter
[Link]("Name from getter: " + [Link]());
}
}
Output:
Name: Rahim | ID: 2219001
Name from getter: Rahim
Prepared by Ashraful Islam Paran
Page 5
Department of CSE, CUET Java OOP Lab Sheet
7. Example 4.2: Encapsulation
class Animal {
private String name;
Animal() {
this("Unknown Animal"); // this() chaining
}
Animal(String name) {
[Link] = name;
}
void eat() {
[Link](name + " is eating.");
}
public String getName() { return name; }
}
class Dog extends Animal {
Dog() {
super(); // Calls Animal() which calls Animal(String)
}
Dog(String name) {
super(name); // Directly call parameterized constructor
}
void bark() {
[Link](getName() + " is barking.");
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
Dog d2 = new Dog("Buddy");
[Link](); [Link]();
[Link](); [Link]();
}
}
Output:
Unknown Animal is eating.
Unknown Animal is barking.
Buddy is eating.
Buddy is barking.
Task 7.1 – Encapsulation & Constructor Chaining (20 min)
a. Create a class Student with private fields: name, id, cgpa.
b. Provide three constructors:
• No-arg → sets default values using this("Unknown", 000, 0.0)
• One String parameter (name only)
Prepared by Ashraful Islam Paran
Page 6
Department of CSE, CUET Java OOP Lab Sheet
• Full three-parameter constructor
c. Use this() for chaining inside the class.
d. Provide public getter methods only (no setters for practice).
e. Test all three constructors in main().
8. Example 5: Method Overriding with super keyword
class Animal {
void sound() {
[Link]("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link](); // Call parent method
[Link]("The dog says: Bow Wow");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
Output:
The animal makes a sound
The dog says: Bow Wow
Task 8.1 – Method Overriding Practice (12 min) Create:
• Employee with method work() → ”Employee works”
• Manager extends Employee and overrides work() to first call [Link]()
then print ”Manager manages team”
Also add a Developer class that extends Employee and prints ”Developer writes
code”.
Prepared by Ashraful Islam Paran
Page 7
Department of CSE, CUET Java OOP Lab Sheet
9. Real-World Example: Employee → Manager (with Encapsu-
lation)
class Employee {
private String name;
private double salary;
Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
void display() {
[Link]("Name: " + name + ", Salary: $" + salary);
}
}
class Manager extends Employee {
private double bonus;
Manager(String name, double salary, double bonus) {
super(name, salary);
[Link] = bonus;
}
@Override
void display() {
[Link]("Manager: " + [Link]().getSimpleName() +
" | Total Pay: $" + (salary + bonus));
}
}
public class Main {
public static void main(String[] args) {
Manager m = new Manager("Karim", 60000, 20000);
[Link]();
}
}
Prepared by Ashraful Islam Paran
Page 8
Department of CSE, CUET Java OOP Lab Sheet
10. Example 10: super to Access Parent Class Variable
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
[Link]("Child x = " + x); // 20
[Link]("Child this.x = " + this.x); // 20
[Link]("Parent x = " + super.x); // 10 KEY
}
}
public class Main {
public static void main(String[] args) {
new Child().display();
}
}
Output:
Child x = 20
Child this.x = 20
Parent x = 10
11. Example 11: super to Call Parent Class Method
class Parent {
void greet() {
[Link]("Hello from Parent");
}
}
class Child extends Parent {
@Override
void greet() {
[Link](); // Calls Parent’s greet()
[Link]("Hello from Child");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
[Link]();
}
}
Output:
Hello from Parent
Hello from Child
Prepared by Ashraful Islam Paran
Page 9
Department of CSE, CUET Java OOP Lab Sheet
12. Example 12: super() – Calling Parent Constructor (No-arg)
class Parent {
Parent() {
[Link]("Parent constructor called");
}
}
class Child extends Parent {
Child() {
super(); // Explicit call (optional if no-arg exists)
[Link]("Child constructor called");
}
}
public class Main {
public static void main(String[] args) {
new Child();
}
}
Output:
Parent constructor called
Child constructor called
13. Example 13: super() with Parameters – Person → Student
class Person {
String name;
Person(String name) {
[Link] = name;
[Link]("Person constructor: " + name);
}
}
class Student extends Person {
int id;
Student(String name, int id) {
super(name); // Must pass name to Parent
[Link] = id;
[Link]("Student constructor: ID = " + id);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Ashraful", 2219022);
}
}
Output:
Person constructor: Ashraful
Student constructor: ID = 2219022
Prepared by Ashraful Islam Paran
Page 10
Department of CSE, CUET Java OOP Lab Sheet
Example 14: Constructor Chaining
class Person {
String name;
int age;
// Constructor 1: No-arg
Person() {
this("Unknown", 0); // chaining inside same class
[Link]("Person() called");
}
// Constructor 2: One-arg
Person(String name) {
this(name, 0); // again chaining
[Link]("Person(String) called");
}
// Constructor 3: Full (main one)
Person(String name, int age) {
[Link] = name;
[Link] = age;
[Link]("Person(String, int) called");
}
}
class Student extends Person {
int roll;
Student() {
super(); // calls Person() which chains down
[Link]("Student() called");
}
Student(String name, int age, int roll) {
super(name, age); // directly call full parent
,→ constructor
[Link] = roll;
[Link]("Student(full) called");
}
}
public class Main {
public static void main(String[] args) {
[Link]("--- Creating Student() ---");
Student s1 = new Student();
[Link]("\n--- Creating Student(full) ---");
Student s2 = new Student("Alice", 20, 101);
}
}
Expected Output:
--- Creating Student() ---
Person(String, int) called
Person(String) called
Prepared by Ashraful Islam Paran
Page 11
Department of CSE, CUET Java OOP Lab Sheet
Person() called
Student() called
--- Creating Student(full) ---
Person(String, int) called
Student(full) called
15. Summary Table
Concept Description Example
Single Inheritance One class extends one class Dog extends Animal
Multilevel Inheritance Chain: C → B → A Puppy → Dog → Animal
Hierarchical Many classes extend one class Dog, Cat extend Animal
Encapsulation private fields + public methods private String name
[Link] Access parent field super.x
[Link]() Call parent method [Link]()
super() Call parent constructor super(name)
this() Call another constructor this(”Unknown”)
Method Overriding Redefine inherited method @Override void sound()
16. Final Task
Design a small system with the following classes using proper inheritance and encapsu-
lation:
• Vehicle (base)
• Car and Bike (derived)
• ElectricCar extends Car
Each class should have private fields, proper constructors, getters/setters, and at least
one overridden method.
17. Practice Questions
1. Explain why Java does not allow multiple inheritance with classes.
2. What is the difference between super and super()?
3. Write a program showing hierarchical inheritance with Vehicle → Car, Bike,
Truck.
4. Create a Person → Student → GraduateStudent multilevel chain with proper
constructors and super().
5. Implement a BankAccount class and SavingsAccount subclass with interest
calculation using overriding.
Prepared by Ashraful Islam Paran
Page 12
Department of CSE, CUET Java OOP Lab Sheet
6. What happens if you place any statement before super() or this() in a con-
structor?
Keep Coding!
Prepared by Ashraful Islam Paran
Page 13