Professional Documents
Culture Documents
Introduction To Data Structures and Abstract Data Types (Adts)
Introduction To Data Structures and Abstract Data Types (Adts)
and
Abstract Data Types (ADTs)
Prepared by:
Dalal Almusalam
2022
ADTs
Abstract Data Types
ADT (Abstract Data Types)
• To drive a car:
• Do you need to know how the engines work?
• Do you know how the fuel discharge system works in a car?
No
• You just need to know how to DRIVE it.
• This is an abstraction
ADT (Abstract Data Types)
In Java, you can implement abstraction by using classes and interfaces.
Abstract Data Types are the datatypes where you can logically work
with the datatype, but you would not know the inner workings of the
datatype.
• Example:
• A person is a human.
• A cat is an animal.
• A car is a vehicle.
Inheritance – "IS-A " relationship
• The syntax of Java Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
Inheritance – "IS-A " relationship
• Programmer is the subclass and Employee is the superclass.
• The relationship between the two classes is:
• Programmer IS-A Employee.
• It means that Programmer is a type of Employee.
Composition vs Inheritance
Composition vs Inheritance
If a Notebook extends
Computer,then the Notebook
is-a Computer
Method Overloading,
Method Overriding,
and Polymorphism
Overloading
• Method overloading is performed within class.
• Parameter must be different.
• Note:
In java, method overloading can't be performed by
changing return type of the method only.
Return type can be same or different in method
overloading. But you must have to change the
parameter.
Overloading
class OverloadingExample{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}
Overriding
• Method overriding is used to provide the specific
implementation of the method that is already provided
by its super class.
• Method overriding occurs in two classes that have IS-
A (inheritance) relationship.
• In case of method overriding, parameter must be
same.
• Return type must be same or covariant in method
overriding.
Overriding
class Animal{
void eat(){
System.out.println("eating...");
}
}
class Dog extends Animal{
void eat(){
System.out.println("eating bread...");
}
}
Overriding
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat();
}
}
Polymorphism
• Polymorphism is one of the OOPs feature that allows us to perform a single action in different
ways.
• Polymorphism is the capability of a method to do different things based on the object that it is
acting upon.
• Types of polymorphism:
1. Method Overloading:
• This is an example of compile time (or static polymorphism)
2. Method Overriding:
• This is an example of runtime time (or dynamic polymorphism)
Demo
public class Main { class Dog extends Animal{
public static void main(String[] args) void eat(){
{ System.out.println("eating meat...");
Animal dog = new Dog(); }
dog.eat(); }
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
An abstract class can extend another Java class and implement multiple Java An interface can extend another Java interface only.
interfaces.
An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }