0% found this document useful (0 votes)
10 views21 pages

Java OOP Concepts: Polymorphism & Inheritance

The document provides an overview of key concepts in Object-Oriented Programming (OOP) in Java, including polymorphism, inheritance, abstraction, the final modifier, and the Object class. It explains polymorphism as the ability of objects to behave differently based on their class type, and outlines various types of inheritance and abstraction. Additionally, it discusses the advantages and disadvantages of polymorphism and the significance of the Object class as the universal superclass in Java.

Uploaded by

nikhildhaka509
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Java OOP Concepts: Polymorphism & Inheritance

The document provides an overview of key concepts in Object-Oriented Programming (OOP) in Java, including polymorphism, inheritance, abstraction, the final modifier, and the Object class. It explains polymorphism as the ability of objects to behave differently based on their class type, and outlines various types of inheritance and abstraction. Additionally, it discusses the advantages and disadvantages of polymorphism and the significance of the Object class as the universal superclass in Java.

Uploaded by

nikhildhaka509
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Presentation :

Submitted To : Dr. Gopal Sir Submitted


By : Nikhil

MCA 1st Sem : 25169


Object-Oriented Programming in Java: Key Concepts :

• Polymorphism

• Multiple Levels of Inheritance

• Abstraction via Abstract Classes

• Final Modifier and the Object Class

• The Universal Superclass: Object Class


What is Polymorphism ?
Polymorphism in Java is one of the core concepts in object-oriented
programming that allows objects to behave differently based on their
specific class type.
The word polymorphism means having many forms, and it comes from the
Greek
words poly (many) and morph (forms), this means one entity can take many
forms.
In Java, polymorphism allows the same method or object to behave differently
based on the
context, specially on the project's actual runtime class.
Example :

class Animal {

void sound() { [Link]("Animal sound"); }

class Dog extends Animal {

void sound() { [Link]("Bark"); } // Overriding

}
Features Of Polymorphism:
• Multiple Behaviors: The same method can behave differently depending on the
object that calls this method.

• Method Overriding: A child class can redefine a method of its parent class.

• Method Overloading: We can define multiple methods with the same name but
different parameters.

• Runtime Decision: At runtime, Java determines which method to call depending on


the object's actual class.
Types of Polymorphism in Java :
1. Compile-Time Polymorphism
Compile-Time Polymorphism in Java is also
known as static polymorphism and also known
as method overloading. This happens when
multiple methods in the same class have the
same name but different parameters.

Note: But Java doesn't support the Operator


Overloading.
2. Runtime Polymorphism

Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a process in which a function call

to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method

Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of

the member functions of the base class. That base function is said to be overridden
Advantage and Disadvantages :

Advantages of Polymorphism Disadvantages of Polymorphism

• Encourages code reuse. • It can make more difficult to


understand the behavior of an object.
• Simplifies maintenance.
• This may cause performance issues, as
• Enables dynamic method dispatch. polymorphic behavior may require
additional computations at runtime
• Helps in writing generic code that
works with many types
Inheritance in Java :

Java Inheritance is a fundamental

concept in OOP(Object-Oriented

Programming). It is the mechanism in

Java by which one class is allowed to

inherit the features(fields and

methods) of another class. In Java,

Inheritance means creating new

classes based on existing ones. A class

that inherits from another class can

reuse the methods and fields of that


Types Of Inheritance :
1. Single Inheritance

In single inheritance, a sub-class


is derived from only one super
class. It inherits the properties
and behavior of a single-parent
class. Sometimes, it is also
known as simple inheritance
2. Multilevel Inheritance

In Multilevel Inheritance, a
derived class will be
inheriting a base class and
as well as the derived class
also acts as the base class
for other classes
3. Hierarchical Inheritance

In hierarchical inheritance, more


than one subclass is inherited
from a single base class. i.e.
more than one derived class is
created from a single base class.
For example, cars and buses
both are vehicle
4. Multiple Inheritance (Through
Interfaces)

In Multiple inheritances, one class can


have more than one superclass and
inherit features from all parent classes.

Note: that Java does not support


multiple inheritances with classes. In
Java, we can achieve multiple
inheritances only through Interfaces
5. Hybrid Inheritance

It is a mix of two or more of the


above types of inheritance. In Java,
we can achieve hybrid inheritance
only through Interfaces if we want to
involve multiple inheritance to
implement Hybrid inheritance.
Abstraction in Java

Abstraction in Java is the process of hiding internal implementation details and


showing only essential functionality to the user. It focuses on what an object does
rather than how it does it.

Abstraction hides the complex details and shows only essential features.

• Abstract classes may have methods without implementation and must be


implemented by subclasses.

• By abstracting functionality, changes in the implementation do not affect the


code that depends on the abstraction
Abstraction Class

An abstract class is a class that cannot be


instantiated and may contain abstract methods
(methods without body) as well as concrete
methods (with implementation).

In shape example, a base type Shape has common


properties like color and size. Specific shapes—
Circle, Square, Triangle—inherit from Shape and
may have unique properties or behaviors. For
instance, some shapes can be flipped, and each
shape calculates its area differently, illustrating
both shared features and individual differences
Example :

abstract class Shape {

abstract void draw();

void display() { [Link]("Shape"); }

class Circle extends Shape {

void draw() { [Link]("Drawing circle"); }

}
Using the Final Modifier

Prevents modification; applies to classes, methods, and variables

Uses:Final class: Cannot be subclassed (e.g., final class Math).

Final method: Cannot be overridden.

Final variable: Constant value (e.g., final int PI = 3.14;)

Implementation :

final class Utility {

final void print() { [Link]("Final


method"); }
}
The Universal Superclass - Object Class

Root of all Java classes; every class


implicitly extends Object

• Key Methods:toString(): String representation.


• equals(): Object comparison.
• hashCode(): Hash value for collections.
• getClass(): Runtime class info.
• clone(): Shallow copy.

You might also like