0% found this document useful (0 votes)
26 views

07-08 Inheritance and Polymorphism

Uploaded by

Michael
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

07-08 Inheritance and Polymorphism

Uploaded by

Michael
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

KENYA INSTITUTE OF SOFTWARE ENGINEERING

OBJECT ORIENTED PROGRAMMIN (java)


INHERITANCE AND POLYMOPHISM
Trainer: Titos Kipkoech

Inheritance in Java
Concept: Inheritance is one of the core principles of Object-Oriented Programming (OOP) in Java,
allowing a new class (subclass) to inherit fields and methods from an existing class (superclass).
This mechanism promotes code reusability and establishes a natural hierarchical relationship
between classes.
Importance:
1. Code Reusability: Allows developers to reuse existing code, reducing redundancy.
2. Maintenance: Easier to maintain and update code since changes in the superclass
automatically propagate to subclasses.
3. Hierarchy Representation: Models real-world relationships naturally, representing "is-a"
relationships.
Reason:
 Inheritance simplifies the software design by allowing classes to be built upon existing ones,
enhancing the abstraction and encapsulation of code.
How to Implement: To create a subclass from a superclass, use the extends keyword.

// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}

// Subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}

Overriding: Overriding allows a subclass to provide a specific implementation of a method already


defined in its superclass. This is achieved by defining a method in the subclass with the same
signature as in the superclass.

class Dog extends Animal {


@Override
void eat() {
System.out.println("The dog eats bones.");
}
}
Polymorphism in Java
Concept: Polymorphism means "many shapes" and allows objects to be treated as instances of their
parent class rather than their actual class. There are two types of polymorphism in Java: compile-
time (method overloading) and runtime (method overriding).
Importance:
1. Flexibility: Polymorphism provides flexibility and integration of multiple forms of object
behavior.
2. Interchangeability: Enables the ability to use different objects interchangeably through a
common interface.
Reason:
 Polymorphism enhances the flexibility and maintainability of the code by allowing one
interface to be used for a general class of actions.
How to Implement:
1. Runtime Polymorphism (Method Overriding): This is achieved when a subclass provides
a specific implementation of a method declared in its superclass.

class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void makeSound() {
System.out.println("Dog barks");
}
}

public class TestPolymorphism {


public static void main(String[] args) {
Animal myAnimal = new Dog(); // Polymorphism
myAnimal.makeSound(); // Outputs: Dog barks
}
}

2. Compile-time Polymorphism (Method Overloading): This is achieved by defining


multiple methods with the same name but different parameter lists within the same class.

class MathOperations {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

public class TestOverloading {


public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(2, 3)); // Outputs: 5
System.out.println(math.add(2.5, 3.5)); // Outputs: 6.0
System.out.println(math.add(1, 2, 3)); // Outputs: 6
}
}

Why and How to Overload: Why:


 Method overloading improves the readability of the code by allowing the same method to
perform different tasks based on parameters.
 It provides a way to handle different data types or a different number of inputs for the same
operation.
How:
 Overload methods by changing the number or type of parameters.

class Printer {
void print(String s) {
System.out.println(s);
}

void print(int i) {
System.out.println(i);
}

void print(String s, int i) {


System.out.println(s + " " + i);
}
}

Summary
Inheritance allows new classes to inherit properties and behavior from existing classes, promoting
code reuse and logical hierarchy. Polymorphism enables methods to do different things based on
the object it is acting upon, providing flexibility and reusability. Together, they form the backbone
of object-oriented design, enabling robust and maintainable software systems.

You might also like