Professional Documents
Culture Documents
13 Abstraction
13 Abstraction
Agenda
● What is Abstraction?
● Ways to achieve Abstraction
● Complete Abstraction Syntax
● Complete Abstraction Example
● Partial Abstraction Syntax
● Partial Abstraction Example
● Interface
● Interface Syntax
● Interface Example
What is Abstraction?
• Abstraction is a process of hiding the implementation details of a class
or method and showing the functionality only to the user.
• Abstraction can be used with classes and methods.
• We cannot create objects for abstract classes.
• To use abstract class, we need to extend abstract class using non
abstract class and create object for non abstract class.
• Now, we can call abstract class functionalities using non abstract class
object.
Ways to achieve Abstraction
● There are two ways to achieve abstraction in java
○ Abstract class (0 to 100%): can achieve abstraction partially or
completely.
○ Interface (100%): gives complete abstraction.
Complete Abstraction Syntax
abstract class ClassName1 class Main{
{ public static void main(String[] args){
public abstract void methodName(); ClassName2 obj = new ClassName2();
} obj.methodName();
}
class ClassName2 extends ClassName1{ }
interface AnimalTravel
{ void travel(); public class Demo
} { public static void main(String args[])
class Animal implements AnimalEat, AnimalTravel {
Animal a = new Animal();
{ public void eat() a.eat();
{ a.travel();
}
System.out.println("Animal is eating");
}
}
public void travel()
{
System.out.println("Animal is travelling");
}
}
Summary
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited