Abstraction in Java

What is abstraction in java

An abstraction is an act of representing essential features and hiding all background details.It is one of the concept of object-oriented programming.It can be achieved by abstract classes or interfaces.

Abstract Methods and Classes

A method declared final modifier ensures that it cannot be redefined we can also make the method to be redefined in subclasses by making overriding compulsory.
     This is done by using the keyword abstract in the method declaration. When a class carries one or more abstract methods it should also be declared abstract.
     An abstract class cannot create an object. The abstract method of an abstract class must be defined in the subclass.
     An abstract cannot be used to declare a constructor or static method.

Example:

abstract class shape
{
abstract void draw()
}
void show()
{
System.out.println(“This is Abstract Class”);
}
class square extends shape
{
void draw()
{
System.out.println(“This is Draw Square”);
}
class circle extends shape
{
void draw()
{
System.out.println(“This is Draw Circle”);
}
class Abstract
{
public static void main()
{
square s = new square();
circle c = new circle();
s.draw();
c.draw();
s.show();
}
}

What is an Interface

Java cannot have more superclass/base class. In such a case if a class with more than one superclass can be implemented using interfaces. An interface is a kind of class. It contains methods and variables but with a difference. The interface defines only abstract methods and final variables. This means that interfaces do not specify any implementation for methods and data fields contain only constants.

Syntax:

interface interfacename
{
variable declaration;
method declaration;
}

Here the interface is a keyword and the interface name is a valid java variable name. The variable declared all constants and the method declaration contains only the method name without any statement.

Example:

interface Area
{
final double pi=3.14;
public void area(int r1);
}
class Circle implements Area
{
public void area(int r1)
{
System.out.println(“Area =”+(pi*r1*r1));
}
}
class demo
{
public static void main(String args[])
{
Circle c = new Circle();
c.area(10);
}
}

     Interfaces can also be extended that is an interface that can be sub interfaced from other interfaces. The new sub-interface will inherit all the members of the super interfaces similar top subclasses this superclass using the keyword extends.

Syntax:

interface interface2 extends interface1
{
body of interface2;
}

     While interfaces are allowed to extends other interfaces sub-interfaces cannot define methods declared in super interfaces. When interfaces are extended to two or more interfaces they are separated by comma(,).

Implementation of Interfaces

     Interfaces are used as superclasses whose classes inherit properties.

Syntax:

 class classname implements interfacename
{
body of class
}

     The class classname implements the interface interfacename. The class can implement more than one interface which is separated by comma(,).

For Example:

interface Area
{
float calculate(float x, floaty)
}
class Rectangle implements Area
{
public float calculate(float x, floaty)
{
return(x,y)
}
class triangle implements Area
{
public float calculate(int x, int y)
{
return((½)*x*y);
}
}
class interdemo
{
public static void main(String args[])
{
Rectangle r = new Rectangle()
System.out.println(“Area of Rectangle”+r.calculate(10,10)
circle c = new circle()
System.out.println(“Area of Triangle”+r.calculate(10,5))
}
}

Accessing Interface Variables

     It is possible to use interfaces to declare a collection of constants that can be used in various classes. Such interfaces do not contain methods those Constant values will be available to any class that implements the interface. The values can be used in any method or anywhere we can use the final value.

For Example:

interface A
{
int m=100;
int n=50;
}
class B implements A
{
int x=m;
}
void method A(int S)
{
…
if(s>n)
{
…
}
}

Related article: https://codingsmania.in/inheritance-in-java/

Leave a Reply