What is difference between abstraction and encapsulation
Answer : Below are difference between abstraction and encapsulation:
Abstraction | Encapsulation |
Abstraction provides a generalization which applied over a set of behaviors | Encapsulation is a technique to hide the implementation details which could be applied either for generic or specialized behavior(s). |
Every method or variable is not an abstraction | Every method or variable is an encapsulation |
Abstraction keeps you focus on what the object does instead of how it does it | Encapsulation lets you focus on how object will behave |
Abstraction is a technique to providing generalization and so a common way to work with objects of vast diversity | Encapsulation is a technique to hide unwanted or un-expected or propriety implementation details from the actual use of object |
Encapsulation example: Below java class you will see how we are hiding property or part of object from its implementation which we can say giving specialize behaviors to part of the object:
package com.javahonk.encapsulationTest; public class EncapsulationTest { public static void main(String[] args) { EncapsulateData encapsulateData = new EncapsulateData(); // ERROR below line : The field EncapsulateData.name is not visible //encapsulateData.name = "Changing your name"; // Only you can get name whatever set in the class System.out.println("Your name: " + encapsulateData.getName()); //We can change name because modifer is public encapsulateData.changeMyName = "Java Monk"; System.out.println("Your change name: " + encapsulateData.getChangeMyName()); } } class EncapsulateData { private String name = "Java Honk"; public String changeMyName = "Java Honk"; public String getName() { return name; } public String getChangeMyName() { return changeMyName; } }
Abstraction example : Below example you will see customer will order Pizza of his choice and finally he will get it Pizza product where he don’t need to bother about how it has been cooked:
package com.javahonk.polymorphism; public class Pizza { String dough; String sauce; String cheese; public Pizza(String dough, String sauce, String cheese) { super(); this.dough = dough; this.sauce = sauce; this.cheese = cheese; } public void getMyPizza() { System.out.println("Your Pizza. Dough: " + dough + " Sauce: " + sauce + " Cheese: " + cheese); } }
package com.javahonk.polymorphism; public class ChickenPizza extends Pizza{ String toppings; public ChickenPizza(String dough, String sauce, String cheese,String toppings) { super(dough, sauce, cheese); this.setToppings(toppings); } public String getToppings() { return toppings; } public void setToppings(String toppings) { this.toppings = toppings; } public void getMyPizza(){ super.getMyPizza(); System.out.println("Chicken Pizza toppings:" +toppings); } }
package com.javahonk.polymorphism; public class VegetablePizza extends Pizza{ String veggieToppings; public VegetablePizza(String dough, String sauce, String cheese, String veggieToppings) { super(dough, sauce, cheese); this.setVeggieToppings(veggieToppings); } public String getVeggieToppings() { return veggieToppings; } public void setVeggieToppings(String veggieToppings) { this.veggieToppings = veggieToppings; } public void getMyPizza(){ super.getMyPizza(); System.out.println("Vegitable Pizza toppings:" +veggieToppings); } }
package com.javahonk.polymorphism; public class BuildPizza { public static void main(String[] args) { Pizza chickenPizza, vegitablePizza; chickenPizza=new ChickenPizza("Classic", "Pizza Sause", "Normal", "Chicken"); vegitablePizza=new VegitablePizza("Modern", "Pizza Sause", "Medium", "Vegitable"); System.out.println("Your chicken pizza details:\n"); chickenPizza.getMyPizza(); System.out.println("\nYour vegitable pizza details:\n"); vegitablePizza.getMyPizza(); } }
Output: