info@tutsfinder.in



Abstraction

Abstraction is a process to hide the implementation details from the users and showing only the functionality. It displays only essential things.

For example: Like ATM Machine, we can see only functionality but don’t know internal details that how it works.

Advantages:
• Increases reusability of code by minimizes code redundancy.
• Separates program into code and implementation.
• Increases code readability.



abstract class Vehicle{        //declare abstract class
    String model;

    public Vehicle(String model){        //abstract class with constructor
    System.out.println("Vehicle constructor called");
    this.model=model;
    }
}
class Car extends Vehicle{
public Car(String model){
    super(model);
    System.out.println("Bike constructor called");
    }
    public String toString(){
    return "Car model is "+super.model;
    }
}
public class Abstraction{
    public static void main(String[] args){
        Vehicle obj = new Car("BMW");
        System.out.println(obj);
        }
    }
}



Output:

Vehicle constructor called
Bike constructor called
Car model is BMW



In java abstraction is achieved in two ways-

• By using abstract class

• Interface


Abstract Class

Abstract class is declared with abstract keyword– it can contain both abstract and non-abstract methods.

Abstract methods are methods that can contain methods without body i.e. undefined methods.

Non-abstract methods are methods that contain methods with body i.e. defined methods.

Abstract class is used to achieve abstraction.

Abstract class cannot be instantiated but can be sub classed.

A simple program to show how abstraction class works in java.

abstract class Vehicle{		//class with abstract keyword
	
	abstract void run();		//abstract method
	void bike(){				//non-abstract method
	System.out.println("Bike is running");
	}
	
}
class Car extends Vehicle{
	void run(){
	System.out.println("Car is running");
	}
}
class AbstractClass{
	public static void main(String[] args){
		Vehicle obj=new Car();
		obj.run();
		obj.bike();
	}	
}
	


Output:

Car is running
Bike is running


Encapsulation

It is mechanism to encapsulate or wrap up all the data into a single entity it is known as encapsulation.

Encapsulation can be achieved in two ways:

1. By using setter and getter methods.

2. Making all variables as private.

For Example: Here ATM Machine is a class. It contains both data i.e. money and operations i.e. withdraw money, balance enquiry and they are integrated in a single entity called ATM. This is called Encapsulation.

class Car{
    private String carColor;
    private String carModel;

    public String getColor()
    {
        return carColor;
    }

    public String getModel()
    {
         return carModel;
    }

    public void setColor(String newColor)
    {
         carColor=newColor;
    }
    public void setModel(String newModel)
    {
        carModel=newModel;
    }
}

class Encapsulation{
    public static void main(String[] args)
    {
        Car obj = new Car();
        obj.setModel("BMW");
        obj.setColor("Black");

        System.out.println(obj.getColor()+" "+obj.getModel()+" looks awesome.");
    }
}


Output:

Black BMW looks awesome.