Hello friends how are you, today in this blog i will teach you what is abstract class and abstract method, how abstract class and abstract method are created , how abstract class and abstract work and many programs using abstract class and abstract in a very simple way.
Let's start
2.It is a collection of abstract method/function or non-abstract method.
3.We can not create instance(object) of an abstract class.
4.We can not define abstract function inside abstract class only can be declared, so it is the responsibility of derived class to implement the method/function of abstract class.
5.An abstract class is also extended by a class.
Example 1 :Abstract class without abstract method
abstract class Geometry
{
/*declaring non-abstract method*/
public void rectangle_area(int height,int width)
{
int ar=height*width;
System.out.println("Area of rectangle="+ar);
}
/*declaring non-abstract method*/
public void square_area(int side)
{
int ar=side*side;
System.out.println("Area of square="+ar);
}
/*declaring non-abstract method*/
public void circle_area(float radius)
{
float ar=3.14f*radius*radius;
System.out.println("Area of circle="+ar);
}
}
/*extending abstract class*/
class Easy extends Geometry
{
public static void main(String[] args)
{
/*creating instance of derived class*/
Easy obj=new Easy();
//calling method of abstract class
obj.rectangle_area(12, 13);
obj.square_area(12);
obj.circle_area(2.2f);
}
}
/*
### Output ###
Area of rectangle=156
Area of square=144
Area of circle=15.197601
*/
Abstract method/function in Java
1.abstract function is declared with abstract keyword.
2.It can be declared only inside abstract class.
3.It can not be defined inside abstract class ,only can be declared so it is the responsibility of derived class to implement abstract method.
Example 1 :Abstract class with abstract method
abstract class Geometry
{
//declaring abstract method
abstract void rectangle_area(int height,int width);
abstract void square_area(int side);
abstract void circle_area(float radius);
}
/*extending abstract class*/
class Easy extends Geometry
{
/*implementing abstrcat method of abstract class*/
public void rectangle_area(int height,int width)
{
int ar=height*width;
System.out.println("Area of rectangle="+ar);
}
/*implementing abstrcat method of abstract class*/
public void square_area(int side)
{
int ar=side*side;
System.out.println("Area of square="+ar);
}
/*implementing abstrcat method of abstract class*/
public void circle_area(float radius)
{
float ar=3.14f*radius*radius;
System.out.println("Area of circle="+ar);
}
public static void main(String[] args)
{
/*creating instance of derived class*/
Easy obj=new Easy();
/*calling abstract method*/
obj.rectangle_area(12, 13);
obj.square_area(12);
obj.circle_area(2.2f);
}
}
/*
### Output ###
Area of rectangle=156
Area of square=144
Area of circle=15.197601
*/
Example 2 :Abstract class with abstract and non-abstract method
abstract class Geometry
{
/*declaring abstract method*/
abstract void rectangle_area(int height,int width);
/*declaring non-abstract or normal method
public void square_area(int side)
{
int ar=side*side;
System.out.println("Area of square="+ar);
}
/*declaring non-abstract or normal method*/
public void circle_area(float radius)
{
float ar=3.14f*radius*radius;
System.out.println("Area of circle="+ar);
}
}
/*extending abstract class*/
class Easy extends Geometry
{
/*implementing abstrcat method of abstract class*/
public void rectangle_area(int height,int width)
{
int ar=height*width;
System.out.println("Area of rectangle="+ar);
}
public static void main(String[] args)
{
/*creating instance of derived class*/
Easy obj=new Easy();
/*calling abstract method*/
obj.rectangle_area(12, 13);
//calling non-abstract or normal method
obj.square_area(12);
obj.circle_area(2.2f);
}
}
/*
### Output ###
Area of rectangle=156
Area of square=144
Area of circle=15.197601
*/
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thanks.😊
0 Comments