3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Explain the basics of designing a class.
- Differentiate between object state and behaviour.
- Explain object composition.
- Explain inheritance as a method of code reuse.
- Explain class hierarchies in Java.
- Declare inheritance in Java.
- Explain inheritance and type casting.
3.2 Overview
Object-oriented programming allows developers to design structured and reusable software applications using classes and objects. Java supports concepts such as composition and inheritance to improve software organization and code reuse.
This lesson introduces learners to class design, object composition, inheritance, and type casting used in Java applications.
These concepts are widely used in:
- enterprise applications,
- banking systems,
- management systems,
- Android applications,
- and software engineering.
Understanding composition and inheritance is important because they support scalable and maintainable software development.
KT0201 — Basics of Designing a Class – Class, Object, State and Behaviour
A class is a blueprint used to create objects.
An object is an instance of a class.
Objects contain:
- state,
- and behaviour.
State
State refers to the data stored in an object.
Example of State
String color;
int speed;
Behaviour
Behaviour refers to actions performed by objects using methods.
Example of Behaviour
void start() {
System.out.println("Fan Started");
}
OOP Example — Fan Class
Deciding State and Constructors
public class Fan {
String color;
int speed;
public Fan(String color, int speed) {
this.color = color;
this.speed = speed;
}
}
OOP Example — Fan Class
Deciding Behaviour with Methods
public class Fan {
void startFan() {
System.out.println("Fan Running");
}
}
Importance of Class Design
Proper class design improves:
- software organization,
- maintainability,
- and reusable development.
KT0202 — Object Composition
Object composition occurs when one object contains another object as part of its structure.
Composition supports:
- modular design,
- reuse,
- and structured relationships.
Customer Address Example
class Address {
String city;
}
class Customer {
Address address = new Address();
}
Books and Reviews Example
A book may contain:
- reviews,
- ratings,
- and comments.
Exercise: Compose a Class Which Addresses All the Aspects of Being Human
Example aspects:
- name,
- age,
- address,
- occupation,
- and health information.
Importance of Composition
Composition improves:
- modularity,
- organization,
- and object relationships.
KT0203 — Inheritance is a Method of Code Reuse
Inheritance allows one class to inherit fields and methods from another class.
Inheritance supports:
- code reuse,
- scalability,
- and hierarchical design.
Example
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
}
Importance of Inheritance
Inheritance reduces:
- duplicated code,
- development time,
- and maintenance effort.
KT0204 — Class Hierarchies
A class hierarchy is a structure showing relationships between parent and child classes.
Example Hierarchy
Animal
|
Dog
Parent Class
The parent class is also called:
- superclass,
- or base class.
Child Class
The child class is also called:
- subclass,
- or derived class.
Importance of Class Hierarchies
Class hierarchies improve:
- organization,
- scalability,
- and software structure.
KT0205 — Java Inheritance Basics
Inheritance allows child classes to access members of parent classes.
What is Inherited?
Child classes inherit:
- fields,
- methods,
- and behaviors.
Example
class Vehicle {
int speed = 100;
}
class Car extends Vehicle {
}
Java Only Supports Singular Inheritance
Java does not support multiple inheritance using classes.
A class can inherit from only one parent class.
Example
class B extends A {
}
Importance of Java Inheritance
Inheritance supports:
- reusable code,
- hierarchical structure,
- and maintainable applications.
KT0206 — Declaring Inheritance in Java
Inheritance is declared using the:
extends
keyword.
Example
class Person {
void display() {
System.out.println("Person Class");
}
}
class Student extends Person {
}
Importance of Declaring Inheritance
The extends keyword creates:
- parent-child relationships,
- reusable structures,
- and organized code.
KT0207 — Inheritance and Type Casting
Type casting converts one type into another compatible type.
In inheritance:
- parent references may refer to child objects.
Example
Animal animal = new Dog();
Upcasting
Upcasting converts:
- child object → parent reference.
Downcasting
Downcasting converts:
- parent reference → child object.
Example
Dog dog = (Dog) animal;
Importance of Type Casting
Type casting supports:
- polymorphism,
- flexible programming,
- and object-oriented design.
3.5 Key Notes / Summary
- Classes define object structures.
- Objects contain state and behaviour.
- Composition combines objects together.
- Inheritance supports code reuse.
- Class hierarchies organize parent-child relationships.
- Java supports singular inheritance.
- The extends keyword declares inheritance.
- Type casting supports polymorphism and flexibility.