Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Introduction to Data Structures

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.

If you define a datatype with the help of a programming language and


then hide its implementation, then it is an abstract datatype. And then you
just present the user with the datatype and the operations for that datatype.
Java ADTs Examples:
• List ADT
• Stack ADT
• Queue ADT
Interface
• An interface specifies or describes an ADT to the applications
programmer:
• The methods and the actions that they must perform
• What arguments, if any, must be passed to each method
• What result the method will return
• The interface can be viewed as a contract which guarantees
how the ADT will function
Interface
• A class that implements the interface provides code for the ADT
• As long as the implementation satisfies the ADT contract, the
programmer may implement it as he or she chooses
• In addition to implementing all data fields and methods in the
interface, the programmer may add:
• Data fields not in the implementation
• Methods not in the implementation
• Constructors (an interface cannot contain constructors because it
cannot be instantiated)
Demo
OOP
Object-Oriented Programming
What Is Reusability
• As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you
create a new class.
• You can use the same fields and methods already defined in the
previous class.
Composition - "has-a" relationship
• The composition relationship of two objects is possible when:
• One object contains another object, and that object is fully dependent on it.
• Example:
• A car has an engine
• A university has a college
• A room has a door 
Composition - "has-a" relationship
• Benefits of using Composition:
• Composition allows us to reuse the code.
• Write a clean code
Demo
Inheritance – "IS-A " relationship 
• parent-child relationship

• 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

is-a (inheritance) has-a (composition)


One class is a subclass of the other class One class has the other class as an
attribute
Composition vs Inheritance
A Computer has only one
Memory

But a Computer is not a


Memory (i.e. not an is-a
relationship)

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(); }

Animal cat = new Cat(); class Cat extends Animal{


cat.eat(); void eat(){
} System.out.println("eating fish...");
} }
}
class Animal{
void eat(){
System.out.println("eating...");
}
}
Abstract Base Classes
• A class which is declared with the abstract keyword is known
as an abstract class in Java.
• It can have abstract and non-abstract methods
• There are two ways to achieve abstraction in java
1.Abstract class (0 to 100%)
2.Interface (100%)
• Example:
abstract class A{
}  
Abstract Base Classes Example
abstract class Shape{   class TestAbstraction1{  
abstract void draw();   public static void main(String args[]){  
Shape s=new Circle();  
}  
s.draw();  
class Rectangle extends Shape{   }  
void draw(){ }  
System.out.println("drawing rectangle");
}  
}  
class Circle extends Shape{  
void draw(){
System.out.println("drawing circle");
}  
}  
Abstract vs Interface
Abstract class Interface
Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.

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. 

An abstract class can have constructors An interface cannot have constructors

Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

You might also like