What Is Abstraction in Java
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?
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.
***
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 {
a.calculate(20, 30);
s.calculate(10, 5);
m.calculate(10, 20);
}
}
Output:
Sum: 50
Subtract: 5
Multiply: 200
Interface in java
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
*/
*/
System.out.println("implementation of method1");
System.out.println("implementation of method2");
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 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.
5) Class that implements any interface must implement all the methods of
that interface, else the class should be declared abstract.
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.
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();
}
}
}
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);
}
}