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

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{ }

public void methodName(){


// code...
}
}
Interface
● Interface will allow us to achieve complete abstraction.
● Non abstract methods cannot be defined in interface class.
● All the methods in the interface needs to be invoked by child class at
least once.
● We use implements keyword to inherit interface class to non abstract or
interface class.
Interface Syntax
Interface class ClassName1
class Main{
{
public static void main(String[] args){
public interface void methodName();
ClassName2 obj = new ClassName2();
}
obj.methodName();
}
class ClassName2 implements ClassName1{
}
public void methodName(){
// code...
}
}
Interface Example
class Main{
interface class Animal{ public static void main(String[] args){
// interface method Dog obj = new Dog();
public interface void makeSound(); obj.makeSound();
public interface void sleep(); obj.sleep();
} }
} Output:
class Dog implements Animal{
Dog Barks
public void makeSound(){
Sleeps as ‘Zzzz’
System.out.println(“Dog Barks”);
}
public void sleep(){
System.out.println(“Sleeps as ‘’Zzzz”);
}
}
Interface Example 2
interface AnimalEat
{
   void eat();
}

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

● We have seen what is abstraction and its types.


● We have also classified Abstraction and seen its types in detail
with syntax and example respectively.
Thank You

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

You might also like