You are on page 1of 19

What Is Abstraction In Java

Abstraction In OOP
In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the
unnecessary details.

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 behaviours of an object differentiate it from other objects of
similar type and also help in classifying/grouping the objects.

For example, when we are driving a car, we are only concerned about driving the car like
start/stop the car, accelerate/ break, etc. We are not concerned about how the actual start/stop
mechanism or accelerate/brake process works internally. We are just not interested in those
details.
What we are concerned about is the “abstract” view of these operations that will help us to propel
the car forward and reach our destination. This is a simple example of abstraction.

Thus the car has all the mechanisms and processes in place but from the end user’s perspective,
i.e. car driver’s perspective he/she will be interested only in the abstract view of these processes.

Abstraction reduces the programming efforts and thereby the complexity. An end-user using the
application need not be concerned about how a particular feature is implemented. He/she can just
use the features as required.

Thus in abstraction, we deal with ideas and not the events. This means that we hide the
implementation details from the user and expose only the functionality to the end-user. Thereby
the user will only know “what it does” rather than “how it does”.
How to achieve abstraction in java?

As abstraction is one of the core principles of Object-oriented programming


practices and Java following all OOPs principles, abstraction is one of the
major building blocks of java language.
In java, abstraction is achieved by interfaces and abstract
classes. Interfaces allows you to abstract the implemetation completely while
abstract classes allow partial abstraction as well.
Data abstraction spans from creating simple data objects to complex
collection implementations such as HashMap or HashSet.
Similarly, control abstraction can be seen from defining simple function calls
to complete open source frameworks. control abstraction is main force
behind structured programming.

abstraction is achieved by interfaces and abstract classes. We can achieve


100% abstraction using interfaces.

Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only


essential information to the user.
Abstraction can be achieved with either abstract
classes or interfaces (which you will learn more about in the next chapter).

The abstract keyword is a non-access modifier, used for classes and


methods:

 Abstract class: is a restricted class that cannot be used to create


objects (to access it, it must be inherited from another class).

 Abstract method: can only be used in an abstract class, and it does


not have a body. The body is provided by the subclass (inherited
from).

1. An abstract class is a class that is declared with an abstract


keyword.
2. An abstract method is a method that is declared without
implementation.
3. An abstract class may or may not have all abstract methods. Some
of them can be concrete methods
4. A method defined abstract must always be redefined in the
subclass, thus making overriding compulsory OR either make the
subclass itself abstract.
5. Any class that contains one or more abstract methods must also be
declared with an abstract keyword.
6. There can be no object of an abstract class. That is, an abstract
class can not be directly instantiated with the new operator.
7. An abstract class can have parameterized constructors and the
default constructor is always present in an abstract class.

When to use abstract classes and abstract methods with an


example

There are situations in which we will 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 will 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.

What is Abstract Class?


Abstract Class is a type of class in OOPs, that declare one or more abstract
methods. These classes can have abstract methods as well as concrete methods. A
normal class cannot have abstract methods. An abstract class is a class that
contains at least one abstract method.

What are Abstract Methods?


Abstract Method is a method that has just the method definition but does not
contain implementation. A method without a body is known as an Abstract
Method. It must be declared in an abstract class. The abstract method will never be
final because the abstract class must implement all the abstract methods.

Advantages of Abstraction
 The main benefit of using an Abstraction in Programming is that it allows
you to group several related classes as siblings.
 Abstraction in Object Oriented Programming helps to reduce the complexity
of the design and implementation process of software.

When to use Abstract Methods & Abstract Class?


Abstract methods are mostly declared where two or more subclasses are also doing
the same thing in different ways through different implementations. It also extends
the same Abstract class and offers different implementations of the abstract
methods.
Abstract classes help to describe generic types of behaviors and object-oriented
programming class hierarchy. It also describes subclasses to offer implementation
details of the abstract class.

***
A class that is declared using “ abstract” keyword is known as abstract
class. It can have abstract methods(methods without body) as well as
concrete methods (regular methods with body). A normal class(non-
abstract class) cannot have abstract methods.
***************************
package com.java.isahasarelationship;

//parent class
abstract class BaseClass
{
//abstract method
abstract public void show1();
//concrete method
public void show2()
{
System.out.println("Concrete method of parent class");
}
}
//child class
class ChildClass extends BaseClass
{
// Must Override this method while extending the Parent class
public void show1()
{
System.out.println("Overriding the abstract method of the parent
class");
}
//Overriding concrete method is not compulsory
/* public void show2()
{
System.out.println("Overriding concrete method of the parent
class");
}*/
}
public class Car {

public static void main(String[] args)


{
/* we can't create object of the parent class hence we are creating
object of the child class */
ChildClass obj = new ChildClass();
obj.show1();
obj.show2();
}
}
Overriding abstract method of the parent class
Overriding concrete method of the parent class
public abstract class MyTest
{
abstract void calculate(int a, int b); // No body.
}
public class Addition extends MyTest
{
void calculate(int a, int b)
{
int x = a + b;
System.out.println(“Sum: ” +x);
}
}
public class Subtraction extends MyTest
{
void calculate(int a, int b)
{
int y = a - b;
System.out.println(“Subtract: ” +y);
}
}
public class Multiplication extends MyTest
{
void calculate(int a, int b)
{
int z = a * b;
System.out.println(“Multiply: ” +z);
}
}
public class MyClass
{
public static void main(String[] args)
{
Addition a = new Addition();
Subtraction s = new Subtraction();
Multiplication m = new Multiplication();

a.calculate(20, 30);
s.calculate(10, 5);
m.calculate(10, 20);
}
}
Output:
Sum: 50
Subtract: 5
Multiply: 200
Interface in java

What is an interface in Java?


Interface looks like a class but it is not a class. An interface can have methods
and variables just like the class but the methods declared in interface are by
default abstract (only method signature, no body, see: Java abstract method).
Also, the variables declared in an interface are public, static & final by default.
We will cover this in detail, later in this guide.

Advantages of interface in java:


Advantages of using interfaces are as follows:

1. Without bothering about the implementation part, we can achieve


the security of implementation
2. In java, multiple inheritance is not allowed, however you can use
interface to make use of it as you can implement more than one
interface.
What is the use of interface in Java?
As mentioned above they are used for full abstraction. Since methods in
interfaces do not have body, they have to be implemented by the class before
you can access them. The class that implements interface must implement all
the methods of that interface. Also, java programming language does not
allow you to extend more than one class, However you can implement more
than one interfaces in your class.

Syntax:
Interfaces are declared by specifying a keyword “interface”. E.g.:

interface MyInterface
{
/* All the methods are public abstract by default
* As you see they have no body
*/
public void method1();
public void method2();
}
Example of an Interface in Java

interface MyInterface

/* compiler will treat them as:

* public abstract void method1();

* public abstract void method2();

*/

public void method1();

public void method2();

class Demo implements MyInterface

/* This class must have to implement both the abstract methods


* else you will get compilation error

*/

public void method1()

System.out.println("implementation of method1");

public void method2()

System.out.println("implementation of method2");

public static void main(String arg[])

MyInterface obj = new Demo();

obj.method1();

Output:

implementation of method1

EX-2:
interface Pet{
public void test();
}
class Dog implements Pet{
public void test(){
System.out.println("Interface Method Implemented");
}
public static void main(String args[]){
Pet p = new Dog();
p.test();
}
}

Interface and Inheritance


As discussed above, an interface can not implement another interface. It has
to extend the other interface. See the below example where we have two
interfaces Inf1 and Inf2. Inf2 extends Inf1 so If class implements the Inf2 it
has to provide implementation of all the methods of interfaces Inf2 as well as
Inf1.

interface Inf1{
public void method1();
}
interface Inf2 extends Inf1 {
public void method2();
}
public class Demo implements Inf2{
/* Even though this class is only implementing the
* interface Inf2, it has to implement all the methods
* of Inf1 as well because the interface Inf2 extends Inf1
*/
public void method1(){
System.out.println("method1");
}
public void method2(){
System.out.println("method2");
}
public static void main(String args[]){
Inf2 obj = new Demo();
obj.method2();
}
}
Key points: Here are the key points to remember about interfaces:
1) We can’t instantiate an interface in java. That means we cannot create the
object of an interface

2) Interface provides full abstraction as none of its methods have body. On the
other hand abstract class provides partial abstraction as it can have abstract
and concrete(methods with body) methods both.

3) implements keyword is used by classes to implement an interface.

4) While providing implementation in class of any method of an interface, it


needs to be mentioned as public.

5) Class that implements any interface must implement all the methods of
that interface, else the class should be declared abstract.

6) Interface cannot be declared as private, protected or transient.

7) All the interface methods are by default abstract and public.

8) Variables declared in interface are public, static and final by default.

interface Try
{
int a=10;
public int a=10;
public static final int a=10;
final int a=10;
static int a=0;
}
All of the above statements are identical.
9) Interface variables must be initialized at the time of declaration otherwise
compiler will throw an error.

interface Try
{
int x;//Compile-time error
}
Above code will throw a compile time error as the value of the variable x is not
initialized at the time of declaration.

10) Inside any implementation class, you cannot change the variables
declared in interface because by default, they are public, static and final. Here
we are implementing the interface “Try” which has a variable x. When we tried
to set the value for variable x we got compilation error as the variable x is
public static final by default and final variables can not be re-initialized.

class Sample implements Try


{
public static void main(String args[])
{
x=20; //compile time error
}
}
11) An interface can extend any interface but cannot implement it. Class
implements interface and interface extends interface.

12) A class can implement any number of interfaces.

13) If there are two or more same methods in two interfaces and a class
implements both interfaces, implementation of the method once is enough.

interface A
{
public void aaa();
}
interface B
{
public void aaa();
}
class Central implements A,B
{
public void aaa()
{
//Any Code here
}
public static void main(String args[])
{
//Statements
}
}
14) A class cannot implement two interfaces that have methods with same
name but different return type.

interface A
{
public void aaa();
}
interface B
{
public int aaa();
}

class Central implements A,B


{

public void aaa() // error


{
}
public int aaa() // error
{
}
public static void main(String args[])
{

}
}
15) Variable names conflicts can be resolved by interface name.

interface A
{
int x=10;
}
interface B
{
int x=100;
}
class Hello implements A,B
{
public static void Main(String args[])
{
/* reference to x is ambiguous both variables are x
* so we are using interface name to resolve the
* variable
*/
System.out.println(x);
System.out.println(A.x);
System.out.println(B.x);
}
}

You might also like