Home Java Overview Maven Tutorials

Sunday 15 January 2017

Abstraction in Java with example

Abstraction in Java with example

For Video : CLICK HERE

1.What is Abstraction in Java?

It is a process of hiding the internal details or we can say implementation details from the user and showing only the required characteristics or the interfaces to the user.

2.How to acheive Abstraction in Java?
In java we can achieve Abstraction by two ways :-
1. Abstract Class
>> 0 to 100% Abstraction

2. Interface
>> 100% Abstraction

3. What is Abstract class & Abstract methods?

Any class which contains the abstract keyword in its declaration, then the class will be an abstract class.

A method without body, means if there is no implementation then it will be an abstract method.

note :- When a class has an abstract methods, then the class should be declared as abstract class.
It is not necessary that every method of an abstract class will be abstract means all the methods will not b abstract.
This is the reason we can acheive 0 to 100 % of abstraction through abstract class as it consist both the abstract and regular methods.

4. How we can use an abstract class?
We need to inherit the abstract class in the sub classes through the extends keyword.

If a class contain any abstract method then the class is declared as abstract class.
An abstract class is never instantiated. It is used to provide abstraction, although it does not provide 100% abstraction because it can also have concrete method.
Java Abstract classes are used to declare common characteristics of subclasses, It can only be used as a superclass for other classes that extend the abstract class.

Abstraction Example :

abstract class Chandan
int z =10;
abstract public int sum(int x, int y);
abstract public int sub(int x, int y);
public class A extends Chandan
public int sum(int x, int y)
public int sub(int x, int y)
public static void main(String args[])
int x = 20;
int z = 30;
A obj = new A();
System.out.println("your sum is :" +obj.sum(4,7));
System.out.println("your subtraction is :" +obj.sub(4,7));
Output :
your sum is :11
your subtraction is :-3

Tags:Abstraction in java with exampleWhat is abstraction in java

No comments:

Post a Comment