Java - Abstraction
Java - 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. Ex: A car is
viewed as a car rather than its individual components.
Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and behaviors of an
object differentiate it from other objects of similar type and also help in classifying/grouping
the objects.
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.
In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100%
abstraction using interfaces.
There are situations in which we want to define a superclass that declares the structure of a
given abstraction without providing a complete implementation of every method. That is,
sometimes we want to create a superclass that only defines a generalization form that will be
shared by all of its subclasses, leaving it to each subclass to fill in the details.
Consider a classic “shape” example, perhaps used in a computer-aided design system or game
simulation. The base type is “shape” and each shape has a color, size and so on. From this,
specific types of shapes are derived (inherited)-circle, square, triangle and so on – each of
which may have additional characteristics and behaviors. For example, certain shapes can be
flipped. Some behaviors may be different, such as calculation of area of a shape.
// Java program to illustrate the
// concept of Abstraction
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;
}
double area() {
return Math.PI * Math.pow(radius, 2);
}
public String toString() {
return "Circle color is " + super.color +
"and area is : " + area();
}
}
class Rectangle extends Shape{
double length;
double width;
public Rectangle(String color,double length,double width) {
// calling Shape constructor
super(color);
System.out.println("Rectangle constructor called");
this.length = length;
this.width = width;
}
double area() {
return length*width;
}
public String toString() {
return "Rectangle color is " + super.color +
"and area is : " + area();
}
}
public class Test
{
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
Output:
Syntax :
interface <interface_name> {
// A simple interface
interface Player
{
final int id = 10;
int move();
}
To implement an interface we use keyword: implements
import java.io.*;
// A simple interface
interface in1
{
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements interface.
class testClass implements in1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Java Programming");
}
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Output:
Java Programming
10
interface vehicleone{
int speed=90;
public void distance();
}
interface vehicletwo{
int distance=100;
public void speed();
}
class MultipleInheritanceUsingInterface{
public static void main(String args[]){
System.out.println("Vehicle");
obj.distance();
obj.speed();
}
}
Output
Output is:
distance travelled is 9000