Csc 303 Continuation
Csc 303 Continuation
PROGRAMMING
INHERITANCE
In object-oriented programming (OOP), Education System
inheritance is a mechanism that allows a class Educational institutions have hierarchical
(called the child class or subclass) to acquire the roles.
properties and behaviors (fields and methods) of Parent Class: Person
another class (called the parent class or Common properties: name, age.
superclass). Common methods: speak(), walk().
Inheritance promotes code reuse, making it
easier to create and maintain applications. Child Classes: Student, Teacher
Student might have study() and takeExam().
Animals and Their Traits Teacher might have teach() and gradePapers()
Different animals share common characteristics
but also have unique features. Banking System
Parent Class: Animal Different types of accounts in a bank share
Common properties: name, age. common features.
Common methods: eat(), sleep(). Parent Class: BankAccount
Common properties: accountNumber, balance.
Child Classes: Dog, Bird, Fish Common methods: deposit(), withdraw().
Dog might have bark() and wagTail().
Bird might have fly() and sing(). Child Classes: SavingsAccount, CurrentAccount
Fish might have swim(). SavingsAccount might add calculateInterest().
CurrentAccount might add overdraftLimit()
class Vehicle {
void start() { Inheritance:
System.out.println("Vehicle starting");
The Car class inherits all the methods and
}
} properties of the Vehicle class.
// Child class (Inherits from Vehicle)
Inheritance allows code reuse, as we don’t have to
class Car extends Vehicle {
void honk() { redefine the start method in the Car class.
System.out.println("Car honking");
Calling Methods:
}
} When calling myCar.start(), Java looks for the start
public class Main {
method in the Car class. Since it’s not explicitly
public static void main(String[] args) {
Car myCar = new Car(); // Create Car object defined there, it finds the method in the parent
myCar.start(); // Call method from Vehicle (parent class)
class (Vehicle).
myCar.honk(); // Call method from Car (child class)
} The myCar.honk() call directly uses the method
} defined in the Car class.
Polymorphism class Calculator {
Polymorphism means "many forms." In Java, it allows // Method with two parameters
one method to behave differently based on the object int add(int a, int b) {
calling it. There are two types of polymorphism: return a + b;
Method Overloading (Compile-time polymorphism): }
Same method name but different parameters.
// Method with three parameters (overloading)
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); //
Output: 15
System.out.println(calc.add(5, 10, 20)); //
Output: 35
Polymorphism class Calculator {
Polymorphism means "many forms." In Java, it allows // Method with two parameters
one method to behave differently based on the object int add(int a, int b) {
calling it. There are two types of polymorphism: return a + b;
Method Overloading (Compile-time polymorphism): }
Same method name but different parameters.
// Method with three parameters (overloading)
Real Life Example: int add(int a, int b, int c) {
1. A calculator can have multiple methods called `add()`. return a + b + c;
If you provide 2 numbers, it adds 2; if you provide 3
}
numbers, it adds 3. The name of the method remains
the same, but it behaves differently depending on the }
input. public class Main {
2. Shape Area Calculation public static void main(String[] args) {
A method to calculate the area of different Calculator calc = new Calculator();
shapes.
Methods Overloaded: System.out.println(calc.add(5, 10)); //
area(int side) for square. Output: 15
area(int length, int breadth) for rectangle. System.out.println(calc.add(5, 10, 20)); //
area(double radius) for circle. Output: 35
Method Overriding (Runtime polymorphism): A method public class Main {
in a subclass overrides a method in the parent class. public static void main(String[] args) {
// Parent class Animal myAnimal = new Animal();
class Animal { myAnimal.sound(); // Output: Animal makes a sound
void sound() { Dog myDog = new Dog();
System.out.println("Animal makes a sound"); myDog.sound(); // Output: Dog barks (Overridden
} method)
} }