Abstraction in Java and Abstract Class & Method in this tutorial we explain about what is abstraction and also explain about abstract method and abstract class in Java with example.
Advantage:-
1.
Avoid code
duplication and increase reusability.
2.
The size of
code is reduce and reduce complexity.
3.
Provide a
security of program only require detail is provide to users.
4.
We can able
to perform any type of changes in back
side without effecting the end users.
Abstract Class:-
Which
is used abstract keyword before the class at the declaration time in a Java is
called abstract class. it can have abstract
and non abstract methods. It cannot be instantiated.
Note:-
1.
We cannot
create a object of abstract class
2.
We use both
abstract and non abstract methods in a abstract class
3.
We can also
use constructor and final method in a constructor in a abstract class.
4.
To use a
abstract class, we have inherit in sub class.
5.
If class has
contain a any abstract method in a class then its compulsory to create that
class as abstract class.
Most Read:-What is Looping statement in Java and explain with example?
Syntax:-
abstract class_name{
--------------------------
----------------------------
}
Abstract Methods:-
If two things different but some of features is same then we use abstract method.
Syntax:- abstract method_name(){}
Example:- two car Maruti and Hyundai and if both cars is red colour then we create a abstract function colour for both of car extends by super class car.
abstract class Car{
public abstract
void colour();
}
class Maruti extends Car{
colour(){
System.out.println(“The Maruti Car Colour is Red”);
}
}
class Hyundai extends Car{
colour(){
System.out.println(“The Hyundai Car Colour is Red”);
}
}
class Main{
public
static void main(){
Maruti maruti =new Maruti(); //
creating a reference of the abstract class for using the function of the
abstract class.
Hyundai hyundai = new Hyundai();
maruti.colour();
hyundai.colour();
}
}
Output:-
The Hyundai Car Colour is Red
0 Comments
Hello Friends, Please comment and share and do not write spam messages