Java Abstraction

Abstraction is a way to provides essential feature and hide implementation process to user That is an important feature of Object oriented programming. In Java programming abstraction are implement by interface and abstract class.

Implementing Abstraction By interface

//Define interface
interface Sports
{
   public void setHomeTeam(String localTeam);
   public void setVisitingTeam(String visitingTeam);
}

class Football implements Sports{

    String localTeam,visitingTeam;
    public void setHomeTeam(String localTeam){
        this.localTeam=localTeam;
    }
    public void setVisitingTeam(String visitingTeam){
        this.visitingTeam=visitingTeam;
    }
    public void matchInfo(){
        System.out.println("\nFootball Match Start between ("+localTeam+" Vs "+visitingTeam+")");
    }

}
 class Cricket implements Sports{

    String localTeam,visitingTeam,location;
    Cricket(String location){
        this.location=location;
    }

    public void setHomeTeam(String localTeam){
        this.localTeam=localTeam;
    }
    public void setVisitingTeam(String visitingTeam){
        this.visitingTeam=visitingTeam;
    }
    public void matchInfo(){
        System.out.println("\nFinal Cricket match between ("+localTeam+" Vs "+visitingTeam+")");
        System.out.println("Location :  "+location);

    }
}
class Operates{

    public static void main(String[] args){

        Cricket ckt = new Cricket("Lord's in London");
        ckt.setHomeTeam("India");
        ckt.setVisitingTeam("Sri Lanka");


        Football ftb = new Football();
        ftb.setHomeTeam("Brazil");
        ftb.setVisitingTeam("Germany");

        ckt.matchInfo();
        ftb.matchInfo();
    }

}

Output

Final Cricket match between (India Vs Sri Lanka)
Location :  Lord's in London

Football Match Start between (Brazil Vs Germany)

Implementing Abstraction By abstract class

In Abstract class are possible to define abstract and non-abstract methods. Abstract method are not contain body part that are implemented by inherited all base class. Abstract class are not create any instance this class properties are used by inherited subclass object.

abstract class Employee{
   //Non Abstract properties    
   String name;
   String type;


   Employee(String name,String type){

        this.name=name;
        this.type=type;

   }

   void detail(double salary){
     System.out.println("name : "+name);
     System.out.println("type : "+type);
     System.out.println("salary : "+salary);
   }
  //Abstract Method
   abstract  double getSlaray();
   abstract  void setSlaray(double amount);
   
}
class FullTime extends Employee{

    double salary;
    public FullTime(String name,String type){
        //Calls Employee class constructor
        super(name,type);
    }
    @Override
    public double getSlaray(){
        return salary;
    }
    @Override
    public void setSlaray(double amount){
        this.salary=amount;
    }
    public void showDetail(){
        super.detail(getSlaray());
    }
    
}

class PartTime extends Employee{
    double salary;
    private int workingHours;
    PartTime(String name,String type,int hours){
        //Calls Employee class constructor
        super(name,type);
        this.workingHours=hours;
    }

    @Override
    public double getSlaray(){
        return workingHours*salary;
    }
    @Override
    public void setSlaray(double amount){
        this.salary=amount;
    }
    public void showDetail(){
        super.detail(getSlaray());
    }

}
class Trainee extends Employee{
    double salary;
    Trainee(String name,String type){
        super(name,type);
    }

    @Override
    public double getSlaray(){
        return salary;
    }
    @Override
    public void setSlaray(double amount){
        this.salary=amount;
    }
    public void showDetail(){
        super.detail(getSlaray());
    }
}
class Operates{

    public static void main(String[] args){
        Trainee trainee = new Trainee("Tom","Trainee"); 
        trainee.setSlaray(5000.3);

        PartTime driver = new PartTime("Smith","Driver",15); 
        driver.setSlaray(1000.5);


        FullTime engineer = new FullTime("Chris","Engineer");
        engineer.setSlaray(300000.76);

        trainee.showDetail();

        driver.showDetail();

        engineer.showDetail();
    }

}
Implementing Abstraction By abstract class

Output

name : Tom
type : Trainee
salary : 5000.3
name : Smith
type : Driver
salary : 15007.5
name : Chris
type : Engineer
salary : 300000.76

Advantages of Abstraction

They are hiding implementation detail and provides secure mechanism. This are provide designing structure of implementation process. And reduce ambiguity of code.

Data Abstraction Vs Encapsulation

Abstraction Encapsulation
Abstraction is based on designing level Encapsulation is based on implementation (development) level
Abstraction is hiding implementation detail to user Encapsulation is hiding data to user
Abstraction is implemented by interface and abstract class in java programming Encapsulation is implemented by used of define private data of class and use this data by class method


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved