Abstraction in Java:
Abstraction is a very important feature of OOPs.
>Abstraction is a process of hiding the internal implementation details and showing only
functionality to the user.
>Another way,it shows only important things to the user and hides the internal details.
>Abstraction focus on what the object does instead of how it does.
Ways to achieve Abstaction:
There are two ways to achieve abstraction in java
>1 Abstract class (provide partial abstraction)
>2 Interface (provide fully abstraction)
Abstract class:
A class that is declared as abstract is known as abstract class.
It needs to be extended and its method implemented.
It cannot be instantiated means we can not create object with new keyword
Note: It has both abstract method and non-abstract method(concrete method means method with body).
Syntax to declare the abstract class:
abstract class <class_name>{}
abstract method:
A method that is declared as abstract and does not have implementation is known as abstract method.
Syntax to define the abstract method:
abstract return_type <method_name>();
Example of abstract class that have abstract method:
In this example,Car the abstract class that contains only one abstract method run. It implementation is
provided by the Maruti class.
abstract class Car{
abstract void run();
}
class Maruti extends Car{
void run() {System.out.println("running...");}
public static void main(String []args){
Car c = new Maruti();
c.run();
}
}
Output: running...
Note: If a class has any method as abstract then we must have to defined that class as abstract.
example:
class Bike{
abstract void run();
}
Output: compile time error
Note: If a class is abstract then it is not necessary that it has abstract method.
Note: If you are extending any abstact class that have abstract method, you must either provide the implementation of all the method or make this (child)class as abstract.
Abstract class having constructor, data member, methods etc:
Note: An abstract class can have abstract method,data member, method body,constructor and even main() method.
example of abstract class that have method body:
abstract class Car{
abstract void run();
void change() {System.out.println("car changed");}
}
class Maruti extends Car{
void run() {System.out.println("maruti is running...");}
public static void main(String []args){
Car obj = new Maruti();
obj.run();
obj.change();
}
}
Output: maruti is running...
car changed
example of abstract class having constructor, field and method:
abstract class Car
{
int speed=50;
Car() {
System.out.println("constructor is called");
}
void show() {System.out.println("it is safari car"); }
abstract void run();
}
class Maruti extends Car{
void run() {System.out.println("maruti running");}
public static void main(String []args){
Car obj = new Maruti();
obj.run();
obj.show();
System.out.println(obj.speed);
}
}
Output: constructor is called
maruti running
it is safari car
50
Another Example of abstract class:
The abstract class can also be used to provide some implementation of the interface. In such case,
the end user may not be forced to override all the methods of the interface.
interface test{
void x();
void y();
void z();
}
abstract class Exam implements test{
public void z() { System.out.println("we am in z method");}
}
class A extends Exam{
public void x(){System.out.println("we am x");}
public void y(){System.out.println("we am y");}
}
class Student{
public static void main(String []args){
test t=new A();
a.x();
a.y();
a.z();
}
}
Output: we am x
we am y
we am in z method
Abstraction is a very important feature of OOPs.
>Abstraction is a process of hiding the internal implementation details and showing only
functionality to the user.
>Another way,it shows only important things to the user and hides the internal details.
>Abstraction focus on what the object does instead of how it does.
Ways to achieve Abstaction:
There are two ways to achieve abstraction in java
>1 Abstract class (provide partial abstraction)
>2 Interface (provide fully abstraction)
Abstract class:
A class that is declared as abstract is known as abstract class.
It needs to be extended and its method implemented.
It cannot be instantiated means we can not create object with new keyword
Note: It has both abstract method and non-abstract method(concrete method means method with body).
Syntax to declare the abstract class:
abstract class <class_name>{}
abstract method:
A method that is declared as abstract and does not have implementation is known as abstract method.
Syntax to define the abstract method:
abstract return_type <method_name>();
Example of abstract class that have abstract method:
In this example,Car the abstract class that contains only one abstract method run. It implementation is
provided by the Maruti class.
abstract class Car{
abstract void run();
}
class Maruti extends Car{
void run() {System.out.println("running...");}
public static void main(String []args){
Car c = new Maruti();
c.run();
}
}
Output: running...
Note: If a class has any method as abstract then we must have to defined that class as abstract.
example:
class Bike{
abstract void run();
}
Output: compile time error
Note: If a class is abstract then it is not necessary that it has abstract method.
Note: If you are extending any abstact class that have abstract method, you must either provide the implementation of all the method or make this (child)class as abstract.
Abstract class having constructor, data member, methods etc:
Note: An abstract class can have abstract method,data member, method body,constructor and even main() method.
example of abstract class that have method body:
abstract class Car{
abstract void run();
void change() {System.out.println("car changed");}
}
class Maruti extends Car{
void run() {System.out.println("maruti is running...");}
public static void main(String []args){
Car obj = new Maruti();
obj.run();
obj.change();
}
}
Output: maruti is running...
car changed
example of abstract class having constructor, field and method:
abstract class Car
{
int speed=50;
Car() {
System.out.println("constructor is called");
}
void show() {System.out.println("it is safari car"); }
abstract void run();
}
class Maruti extends Car{
void run() {System.out.println("maruti running");}
public static void main(String []args){
Car obj = new Maruti();
obj.run();
obj.show();
System.out.println(obj.speed);
}
}
Output: constructor is called
maruti running
it is safari car
50
Another Example of abstract class:
The abstract class can also be used to provide some implementation of the interface. In such case,
the end user may not be forced to override all the methods of the interface.
interface test{
void x();
void y();
void z();
}
abstract class Exam implements test{
public void z() { System.out.println("we am in z method");}
}
class A extends Exam{
public void x(){System.out.println("we am x");}
public void y(){System.out.println("we am y");}
}
class Student{
public static void main(String []args){
test t=new A();
a.x();
a.y();
a.z();
}
}
Output: we am x
we am y
we am in z method
No comments:
Post a Comment