Lecture - 10-Abstraction in Java
Lecture - 10-Abstraction in Java
Lecture - 10-Abstraction in Java
• 1. Abstraction in Java
• 2. Ways to achieve Abstraction
• 2.1. Abstract Classes and Abstract Methods
• 2.2. Interface
• 3. Advantage of Abstraction
• 4. Useful Links
2
Topic - 1 : Abstraction in Java
3
Topic - 1 : Abstraction
4
Topic - 2 : Ways to achieve Abstraction
5
Topic - 2 : Ways to achieve Abstraction
In Java, we can achieve Data Abstraction using Abstract classes and interfaces.
• Interfaces allow 100% abstraction (complete abstraction). Interfaces allow you to
abstract the implementation completely.
7
2.1: Abstract Class
8
Points to Remember
9
Syntax of declaring an abstract class:
• To declare an abstract class we use the keyword abstract. The syntax is given
below:
access-specifier abstract class ClassName
{
//class body
}
10
Abstract Methods in Java
• A method which is declared as abstract and does not have implementation is
known as an abstract method.
• The declaration of an abstract method must end with a semicolon ;
• The child classes which inherit the abstract class must provide the
implementation of these inherited abstract methods.
• Syntax of declaring abstract methods:
11
Example – 1
An abstract class that has abstract method
public abstract class Vehicle { public class Car extends Vehicle {
// Outputs
Car moves faster.
12
Example – 2
An abstract class that has abstract and non-abstract methods
} // Outputs:
Vehicle is Created.
Car moves faster.
All Vehicle carry loads 14
UML representation
+ Vehicle()
+ move() : void Italic font
+ carry () : void
extends
Car
+ move() : void
+ main(String[]):void
15
Example – 4 : Home Work
Vehicle
+ Vehicle()
+ move() : void
+ carry () : void
Car Boat
16
Animal
Example – 5 :
+ eat() : void
Home Work + move() : void
+ life() : void
Main
+main(String [] ) : void
17
Topic – 2.2 : Interface in Java
18
2.2: Interface in Java
• It is much similar to the class but the only difference is that it has abstract methods
and static constants.
• Multiple-Inheritance supported.
19
Internal addition by the compiler
20
Syntax of declaring an Interface
interface <interface_name>{
// declare constant fields
21
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface
22
Example – 6
// Outputs
I am drawing a Line
23
UML representation
<<Drawing>>
+ draw() : void
implements
Line
+ draw() : void
+ main(String[]):void
24
UML representation
<<Drawing>>
+ draw() : void
implements
Line Circle
Main
+ main(String[]):void
25
Example – 7: Find out the output and draw the UML
+ play() : void
+ stop() : void
+ pause() : void
+ reverse() : void
implements
CDPlayer DVDPlayer
27
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as
multiple inheritance
28
Example – 9
class DemoClass implements FirstInterface, SecondInterface {
interface FirstInterface {
public void myMethod(); public void myMethod() {
} System.out.println("Some text..");
}
class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
} 29
Some helpful Links
• https://techvidvan.com/tutorials/abstraction-in-java/
• https://www.javatpoint.com/abstract-class-in-java
• https://techvidvan.com/tutorials/java-interface/
30
Thank you!
31