You are on page 1of 5

Title of The Report:- Polymorphism in Java

and Abstract Data Types and their


specifica on in java.
Introduc on
 Polymorphism and Abstract Data Types (ADTs) are fundamental
concepts in computer science and programming.

 In the context of Java, polymorphism allows objects of different


classes to be treated as objects of a common superclass, while
ADTs provide a high-level descrip on of data and opera ons,
abstrac ng away implementa on details.

 This report discusses the concepts of polymorphism and ADTs


in Java and how they are specified and implemented.

Polymorphism in Java
 Polymorphism is one of the four fundamental principles of
object-oriented programming (OOP), alongside encapsula on,
inheritance, and abstrac on.

 It allows different objects to be treated as instances of a


common superclass, enabling flexibility and extensibility in
code.

Types of Polymorphism in Java


1. Compile-Time Polymorphism (Sta c Binding): This occurs
when the method to be called is determined at compile
me. It is achieved through method overloading, where
mul ple methods in a class have the same name but
different parameter lists.

2
public class MathOpera ons {
public int add(int a, int b) {
return a + b;
}

public double add(double a, double b) {


return a + b;
}
}

In this example, the ‘add’ method is overloaded to handle both


‘int’ and ‘double’ data types.

2. Run-Time Polymorphism (Dynamic Binding): This occurs


when the method to be called is determined at run me. It
is achieved through method overriding, where a subclass
provides a specific implementa on of a method declared
in its superclass.

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

class Dog extends Animal {


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

Here, the ‘Dog’ class overrides the ‘makeSound’ method


defined in the ‘Animal’ class.
3
Benefits of Polymorphism
 Code reusability: Polymorphism allows you to write
generic code that can work with objects of different
classes.
 Extensibility: You can easily add new classes that extend
exis ng ones without modifying the exis ng code.
 Flexibility: Polymorphism allows for more dynamic and
adaptable code.

Abstract Data Types (ADTs) in Java


 An Abstract Data Type (ADT) is a high-level descrip on of a data
structure and the opera ons that can be performed on it,
without specifying the implementa on details.

 Java provides a way to define ADTs using interfaces and abstract


classes.

Specifying ADTs in Java


1. Interface: An interface defines a contract for a class to
implement. It contains method signatures without any
implementa on. Interfaces are used to specify the opera ons
an ADT should support.
public interface List<E> {
void add(E element);
E get(int index);
int size();
}

In this example, the ‘List’ interface specifies the methods ‘add’, ‘get’,
and ‘size’ that any list-based ADT should implement.

4
2. Abstract Class: An abstract class is a class that cannot be
instan ated but can contain both method declara ons and
par al implementa ons. Abstract classes are useful for
providing a common base for ADT implementa ons.
public abstract class AbstractList<E> implements List<E> {
protected int size;

@Override
public int size() {
return size;
}
}

Here, ‘AbstractList’ provides a par al implementa on of the ‘size’


method, which can be shared by concrete list implementa ons.

Implemen ng ADTs
To implement an ADT in Java, you create concrete classes that extend
abstract classes or implement interfaces. For example, you can
implement a ‘LinkedList’ class that implements the ‘List’ interface.

public class LinkedList<E> extends AbstractList<E> {


// Implementa on details for add, get, and other methods.
}

5
Benefits of ADTs
 Abstrac on: ADTs allow you to work with data structures
at a high level, ignoring implementa on details.
 Code organiza on: ADTs help organize code by separa ng
the interface from the implementa on.
 Reusability: Once an ADT is defined, it can be used to
implement various data structures without rewri ng the
same opera ons.

Conclusion
 Polymorphism and Abstract Data Types are essen al
concepts in Java programming.
 Polymorphism enables code flexibility and reuse, while
ADTs provide a high-level specifica on for data structures
and opera ons, promo ng code abstrac on and
reusability.
 Understanding and effec vely using these concepts can
lead to more maintainable and extensible Java programs.

References
 h ps://www.geeksforgeeks.org/polymorphism-in-java/
 h ps://www.mygreatlearning.com/blog/polymorphism-in-
java/
 h ps://www.upgrad.com/blog/types-of-polymorphism-in-
java/
 h ps://www.geeksforgeeks.org/abstract-data-types/
 h ps://web.mit.edu/6.005/www/sp15/classes/12-abstract-
data-
types/#:~:text=Abstract%20data%20types%20are%20characte
rized,adequate%2C%20and%20representa on%2Dindepende
nt.

You might also like