Abstraction is method of hiding the implementation details and showing only the functionality, basicallyProvides guidelines to build a standard product.
Example –
You know how a car look like and how to drive, but you don’t know how to build a car.
For detailed explanation –
Types of abstraction –
Abstraction can be achieved by 2 types
- Abstract
- Interface
Abstract –
- Abstract class will have mix of implemented and un implemented methods
- Syntax – abstract class ClassName
- An abstract class may or may not contain abstract methods
- If at least one method is abstract, then the class should be declared as abstract
- Can not create an object of an Abstract class
- Child class that inherit abstract class with abstract methods, should implement all the abstract methods
- Abstract class can have constructors
Code implementation –
Shape.java
—-Circle.java
—-Rectangle.java
package abstraction; public abstract class Shape { String Color; double Area; Shape(String Color){ this.Color = Color; System.out.println("Color of shape - " + this.Color); } abstract double calcArea(); void DispArea(double Area){ this.Area = Area; System.out.println("Area - " + this.Area); } }
package abstraction; public class Circle extends Shape{ double Area; double pi = 3.14; double radius; Circle(String Color) { super(Color); } @Override double calcArea() { Area = pi * radius * radius; return Area; } public static void main(String[] args) { Circle circle = new Circle("Green"); circle.radius = 5; circle.DispArea(circle.calcArea()); } }
package abstraction; public class Rectangle extends Shape{ double Area; double length; double breath; Rectangle(String Color) { super(Color); } @Override double calcArea() { Area = length * breath; return Area; } public static void main(String[] args) { Rectangle rect = new Rectangle("Red"); rect.length = 5; rect.breath = 10; rect.DispArea(rect.calcArea()); } }
Interface –
- Interface should have all unimplemented methods.
- Syntax – interface InterfaceName
- All the methods in interface are abstract
- Can not create an object of an interface
- All the fields of an interface must be declared as static final
- An interface is implemented by a class
- An interface is extended by multiple interface
Code implementation –
package interfaceEx; interface Shape { double calcArea(); String GetColor(); void DispColor(); void DispArea(); }
package interfaceEx; public class Circle implements Shape{ double Area; double pi = 3.14; double radius; @Override public double calcArea() { Area = pi * radius * radius; return Area; } @Override public String GetColor() { return "Green"; } @Override public void DispColor() { System.out.println(GetColor()); } @Override public void DispArea() { System.out.println(calcArea()); } public static void main(String[] args) { Circle circle = new Circle(); circle.radius = 4; circle.GetColor(); circle.DispColor(); circle.DispArea(); } }
package interfaceEx; public class Rectangle implements Shape{ double Area; double length; double breath; @Override public String GetColor() { return "Green"; } @Override public double calcArea() { Area = length * breath; return Area; } @Override public void DispColor() { System.out.println(GetColor()); } @Override public void DispArea() { System.out.println(calcArea()); } public static void main(String[] args) { Rectangle rect = new Rectangle(); rect.length = 5; rect.breath = 10; rect.DispArea(); rect.DispColor(); } }