UNIT 3b Polymorphism
UNIT 3b Polymorphism
Concept
Polymorphism, a fundamental concept in object-oriented programming (OOP), empowers objects to behave as instances of their
parent class rather than their specific type. This dynamic adaptability, derived from the Greek words "poly" (many) and "morph"
(form), allows a single action to exhibit diverse behavior depending on the object it interacts with.
Compile-Time Polymorphism: Method Overloading
4 Example
Consider a class named `Calculator` with multiple methods for addition. One method could take two integer arguments, while
another could accept two double-precision floating-point arguments. This allows for a consistent `add` method name, simplifying its
use in various scenarios.
class Calculator {
int add(int a, int b) {
return a + b;
}
2 Overriding Methods
Method overriding occurs when a subclass provides a unique implementation for a method that was originally defined in
its superclass. This redefined method in the subclass shares the same name and parameter list as the superclass method.
The subclass's version takes precedence over the superclass's version.
4 Example
Imagine a superclass named `Animal` with a method called `makeSound()`. A subclass `Dog` overrides this method,
providing a specific implementation to bark. When a reference of type `Animal` points to an object of type `Dog`, calling
class Animal {
void sound() {
System.out.println("Generic animal sound");
}
}
animal1.sound();
animal2.sound();
}
}
Abstract Classes in Java
In the world of object-oriented programming, abstract classes play a crucial role in creating flexible and reusable code. These classes,
declared using the **abstract** keyword, serve as blueprints for subclasses, defining common features and behaviors that can be
inherited and customized. Abstract classes are powerful tools for achieving abstraction, code reusability, and polymorphism in Java.
The Essence of Abstract Classes
1 Uninstantiable Entities 2 Abstract and Concrete 3 Constructors and Static
Methods Members
Abstract classes are not meant to
be instantiated directly. You An abstract class can contain both Although you cannot create
cannot create an object of an abstract methods, which are objects of an abstract class, it can
abstract class. Their purpose is to declared without implementation, still have constructors.
serve as a foundation for concrete and non-abstract methods, which Constructors are used during the
subclasses, which will inherit their have a concrete body of code. initialization of subclasses.
properties and methods. Abstract methods must be Abstract classes can also have
overridden by subclasses, while static members, which are shared
non-abstract methods can be used among all instances of the class
directly or overridden if needed. and its subclasses.
An abstract method is declared without Any subclass inheriting an abstract Abstract methods are crucial for
an implementation. It uses the class must provide implementations for enforcing a specific behavior or
**abstract** keyword and is simply a all the abstract methods declared in the functionality that must be implemented
placeholder, a contract that must be parent class. If a subclass doesn't in all subclasses. They allow you to
fulfilled by subclasses. The subclass override all abstract methods, it must define a common pattern without
must override the abstract method and also be declared as abstract, indicating dictating the exact implementation
provide its own implementation. its incompleteness. details.
Benefits of Abstract Classes
Abstraction
Abstract classes help in achieving abstraction by hiding implementation details and exposing only the essential features.
This promotes modularity and makes code easier to understand and maintain.
Code Reusability
By defining common methods in an abstract class, you can reuse those methods across multiple subclasses. This reduces
redundancy and simplifies code development.
Polymorphism
Abstract classes enable polymorphism, which allows you to treat objects of different subclasses as if they were objects of
the abstract class. This makes your code more flexible and adaptable.
Real-World Application: Shape Example
Abstract Class Shape
In this example, the Shape abstract class defines the common properties and behaviors of all shapes, such as the ability to calculate
area and perimeter. Concrete subclasses like Circle, Square, Rectangle, and Triangle inherit these properties and implement the abstract
methods based on their specific shape characteristics.
abstract class Animal {
abstract void makeSound();
void eat() {
System.out.println("I can eat.");
}
}