OOPS concepts in Java
Java tutorials for beginners
Java basics concepts
Java Objects and Classes
What is Inheritance in Java?
What is Abstraction in Java?
What is Interface in Java?
What is Abstract Class in Java?
What is Polymorphism in Java?
What is Encapsulation in Java?
Java programming example
How Java Program Work?
Hello World in Java
Java basics for beginners
Abstraction:
Abstraction is the process of hiding the implementation details, only showing the functionality.
In other words, Abstraction is the process to reduce complexity and taking out the important characteristics of a class.
There are two ways to achieve abstraction in java
1. Abstract class
1. Abstract class
2. Interface
1. Abstract class
Abstract class
defines a class that contains the abstraction without providing a complete
implementation of method. The abstract keyword is used for abstract class and
abstract method.
Abstract classes are used if you want to create a super class
that only defines the variables and methods and sub class will be accessed the
variables and methods for implementation.
Abstract class
defines a class that contains the abstraction without providing a complete
implementation of method. The abstract keyword is used for abstract class and
abstract method.
Abstract classes are used if you want to create a super class
that only defines the variables and methods and sub class will be accessed the
variables and methods for implementation.
Rule of Abstract class.
1. Abstract classes may or may not contain abstract methods.
2. Must be
declared abstract keyword if a class has at least one abstract method.
3. The class
cannot be instantiated if abstract is declared.
4. Must be
provided implementations of all the abstract methods, If you inherit
an abstract class.
Example:
abstract class Figure {
double dim1;
double dim2;
Figure(double d1,double d2){
dim1
= d1;
dim2
= d2;
}
abstract void area();
}
class Rectangle extends Figure{
Rectangle(double d1,douple d2){
super(d1,d2);
}
//Override area of rectangle
void area(){
System.out.println("area
of rectangle is"+ dim1*dim2);
}
}
class Demo{
public static void main(String
args[]){
Figure
f = new Rectangle(4,7);
f.area();
}
Output:
area
of rectangle is 28
2. Interface
1. Abstract classes may or may not contain abstract methods.
2. Must be
declared abstract keyword if a class has at least one abstract method.
3. The class
cannot be instantiated if abstract is declared.
4. Must be
provided implementations of all the abstract methods, If you inherit
an abstract class.
Example:
abstract class Figure {
double dim1;
double dim2;
Figure(double d1,double d2){
dim1
= d1;
dim2
= d2;
}
abstract void area();
}
class Rectangle extends Figure{
Rectangle(double d1,douple d2){
super(d1,d2);
}
//Override area of rectangle
void area(){
System.out.println("area
of rectangle is"+ dim1*dim2);
}
}
class Demo{
public static void main(String
args[]){
Figure
f = new Rectangle(4,7);
f.area();
}
Output:
area
of rectangle is 28
2. Interface
The
interface in java provides abstraction. We can create only abstract
method in interface and can’t create implementation or method body. It
is used to achieve abstraction and multiple inheritance in Java. It has
static constants and abstract methods.
Rule of Interface
1. All interface Methods are implicitly public and abstract
2. All variables in Interface by default constant(public, static, final).
3. Interface Methods can not be static.
4. Interface Methods can not be final.
5. One Interface can extend one or more Interface.
Example:
interface
Area
{
final static float pi = 3.14f;
float findArea(float x);
}
class Circle implements Area
{
public float findArea(float x)
{
return (x*x*xpi);
}
}
class Square implements Area
{
public float findArea(float x)
{
return(x*x);
}
}
public class InterFaceDemo
{
public static void main(String args[])
{
Circle c = new Circle();
Square s= new Square();
Area area;
area = c;
System.out.println("Area of Circle
is" + area.findArea(10));
area = s;
System.out.println("Area of Square
is" + area.findArea(30));
}
The
interface in java provides abstraction. We can create only abstract
method in interface and can’t create implementation or method body. It
is used to achieve abstraction and multiple inheritance in Java. It has
static constants and abstract methods.
Rule of Interface
1. All interface Methods are implicitly public and abstract
2. All variables in Interface by default constant(public, static, final).
3. Interface Methods can not be static.
4. Interface Methods can not be final.
5. One Interface can extend one or more Interface.
Example:
interface
Area
{
final static float pi = 3.14f;
float findArea(float x);
}
class Circle implements Area
{
public float findArea(float x)
{
return (x*x*xpi);
}
}
class Square implements Area
{
public float findArea(float x)
{
return(x*x);
}
}
public class InterFaceDemo
{
public static void main(String args[])
{
Circle c = new Circle();
Square s= new Square();
Area area;
area = c;
System.out.println("Area of Circle is" + area.findArea(10));
area = s;
System.out.println("Area of Square is" + area.findArea(30));
}
{
final static float pi = 3.14f;
float findArea(float x);
}
class Circle implements Area
{
public float findArea(float x)
{
return (x*x*xpi);
}
}
class Square implements Area
{
public float findArea(float x)
{
return(x*x);
}
}
public class InterFaceDemo
{
public static void main(String args[])
{
Circle c = new Circle();
Square s= new Square();
Area area;
area = c;
System.out.println("Area of Circle is" + area.findArea(10));
area = s;
System.out.println("Area of Square is" + area.findArea(30));
}