Skip to content

Latest commit

 

History

History
65 lines (50 loc) · 1.88 KB

File metadata and controls

65 lines (50 loc) · 1.88 KB

Abstraction

Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user.

Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is.

Example Code

abstract class Shape  
{ 
String color; 

// these are abstract methods 
abstract double area(); 
public abstract String toString(); 
  
// abstract class can have constructor 
public Shape(String color) { 
    System.out.println("Shape constructor called"); 
    this.color = color; 
} 
  
// this is a concrete method 
public String getColor() { 
    return color; 
} 
} 

class Circle extends Shape 
{ 
double radius; 
  
public Circle(String color,double radius) { 

    // calling Shape constructor 
    super(color); 
    System.out.println("Circle constructor called"); 
    this.radius = radius; 
} 

@Override
double area() { 
    return Math.PI * Math.pow(radius, 2); 
} 

@Override
public String toString() { 
    return "Circle color is " + super.color +  
                   "and area is : " + area(); 
}  
public class Test  
{ 
public static void main(String[] args) 
{ 
    Shape s1 = new Circle("Red", 2.2);  
      
    System.out.println(s1.toString()); 
} 
} 

Output:

Shape constructor called
Circle constructor called
Circle color is Red and area is : 15.205308443374602