![]() |
Java interface Class Abstract Method with Example in 2021 |
Java interface Class Abstract Method with Example
Java interface: A Java interface is a very important element in java. It is called reference type interface is a blueprint of a class. It is a collection of abstract methods and functions.It can be used declared with interface keyword. We can not create an object of the interface.
Which can not define a function inside the interface.
so it is the responsibility of the derived class to implement the method and function of the interface.
A class is extended by a class but an interface is implemented by a class.
How To Use Interface Class in Java?
interface Geometry
{
void rectangle_area(int h,int w);
void square_area(int side);
void circle_area(float radius);
}
class icoderweb implements Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(12, 12);
obj.square_area(10);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:144
Area of Square:100
Area of Circle:15.197601
Abstract Class: A Java Abstract class is an important element in java that is declared with abstract keyword.
Its collection of abstract methods and function or non-abstract methods.
{
public void rectangle_area(int height,int width)
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
}
class icoderweb extends Geometry
{
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
class obj.rectangle_area(17, 17);
obj.square_area(13);
obj.circle_area(2.2f);
}
}
Area of Square:169
Area of Circle:15.197601
{
abstract void rectangle_area(int h,int w);
abstract void square_area(int side);
abstract void circle_area(float radius);
}
class Easy extends Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
Area of Square:196
Area of Circle:15.197601
{
abstract void rectangle_area(int h,int w);
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
}
}
class icoderweb extends Geometry
{
public void rectangle_area(int h,int w)
{
int ar=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
Area of Circle:15.197601
Abstract class: Abstract class is an important element in java that is declared with abstract keyword. It is a collection of abstract methods and functions or non-abstract methods. We can not create objects of an abstract class.
We can not define an abstract function inside the abstract class only can be declared, so it is the responsibility of the derived class to implement the method/function of an abstract class. An abstract class is also extended by a class.
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
}
}
class icoderweb extends Geometry
{
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
class obj.rectangle_area(17, 17); obj.square_area(14);
obj.circle_area(2.2f);
}
}
Area of Square:196
Area of Circle:15.197601
abstract void square_area(int side);
abstract void circle_area(float radius);
}
class icoderweb extends Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
Area of Square:196
Area of Circle:15.197601
{
abstract void rectangle_area(int h,int w);
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
}
}
class icoderweb extends Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
Area of Square:196
Area of Circle:15.197601
{
int h=20;
public void area()
{
int h=10;
int w=10;
int area=h*w;
System.out.println("Area of Rectangle 1:"+area);
area=this.h*this.w;
System.out.println("Area of Rectangle 2:"+area);
}
}
class icoderweb
{
public static void main(String[] args)
{
Rectangle obj=new Rectangle();
obj.area();
}
}
Area of Rectangle 2:400
Java this keyword Using With Function: Java using this keyword we can also call a function of class without creating an object of that class.
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
this.rectangle_area(17, 17);
}
}
class icoderweb
{
public static void main(String[] args)
{
Geometry obj=new Geometry();
obj.square_area(14);
}
}
Area of Square:196
Java this keyword constructor: Java using this keyword we can also call a function of class without creating an object of that class.
{
Rectangle(int h,int w)
{
int area=h*w;
System.out.println("Area of rectangle:"+area);
}
public Rectangle()
{
this(17,17);
System.out.println("I am inside Default Constructor");
}
}
class icoderweb
{
public static void main(String[] args)
{
Rectangle obj=new Rectangle();
}
}
I am inside Default Constructor
We can not create objects of an abstract class. We can not define an abstract function inside the abstract class only can be declared.
so it is the responsibility of the derived class to implement the method and function of an abstract class. An abstract class is also extended by a class.
👉Java Class Object
👉Java Abstraction Inheritance
Some Others Related Topic in Java Programming
👉 Java Exception👉Java Class Object
👉Java Abstraction Inheritance
How To Use Abstract class without abstract method in Java?
abstract class Geometry
{
public void rectangle_area(int height,int width)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
}
class icoderweb extends Geometry
{
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
class obj.rectangle_area(17, 17);
obj.square_area(13);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:169
Area of Circle:15.197601
Abstract method: A abstract function is declared with an abstract keyword. It can be declared only inside the abstract class. It can not be defined inside the abstract class only can be declared so it is the responsibility of the derived class to implement the abstract method.
How To Use Abstract class with the abstract method in Java?
abstract class Geometry
{
abstract void rectangle_area(int h,int w);
abstract void square_area(int side);
abstract void circle_area(float radius);
}
class Easy extends Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:196
Area of Circle:15.197601
An abstract class with an abstract and non-abstract method
abstract class Geometry
{
abstract void rectangle_area(int h,int w);
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
}
class icoderweb extends Geometry
{
public void rectangle_area(int h,int w)
{
int ar=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:196
Area of Circle:15.197601
Abstract class: Abstract class is an important element in java that is declared with abstract keyword. It is a collection of abstract methods and functions or non-abstract methods. We can not create objects of an abstract class.
We can not define an abstract function inside the abstract class only can be declared, so it is the responsibility of the derived class to implement the method/function of an abstract class. An abstract class is also extended by a class.
Java Abstract class Using without Abstract Method
abstract class Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
}
class icoderweb extends Geometry
{
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
class obj.rectangle_area(17, 17); obj.square_area(14);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:196
Area of Circle:15.197601
Abstract method and function: Abstract function is declared with abstract keyword. It can be declared only inside the abstract class.
It can not be defined inside the abstract class only can be declared so it is the responsibility of the derived class to implement the abstract method.
How To Use Abstract class with the abstract method in Java?
abstract class Geometry
{
abstract void rectangle_area(int h,int w);
abstract void rectangle_area(int h,int w);
abstract void square_area(int side);
abstract void circle_area(float radius);
}
class icoderweb extends Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:196
Area of Circle:15.197601
How To Use Abstract class with the abstract and non-abstract methods in Java?
abstract class Geometry
{
abstract void rectangle_area(int h,int w);
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
}
public void circle_area(float radius)
{
float area=3.14f*radius*radius;
System.out.println("Area of Circle:"+area);
}
}
class icoderweb extends Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.rectangle_area(17, 17);
obj.square_area(14);
obj.circle_area(2.2f);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:196
Area of Circle:15.197601
This Keyword: A java is this keyword that refers to the current object of the class. It can be used to replace the local variables with class-level variables. It is also used to call the current class method and function. It is also used to call a current class overloaded constructor.
Java this keyword variable hiding: Java using this keyword we can replace the local variable with an instance variable.
Java this keyword variable hiding: Java using this keyword we can replace the local variable with an instance variable.
How To Variable Hide Using this Keyword in Java?
class Rectangle
{
int h=20;
int w=20;
public void area()
{
int h=10;
int w=10;
int area=h*w;
System.out.println("Area of Rectangle 1:"+area);
area=this.h*this.w;
System.out.println("Area of Rectangle 2:"+area);
}
}
class icoderweb
{
public static void main(String[] args)
{
Rectangle obj=new Rectangle();
obj.area();
}
}
*****OUTPUT*****
Area of Rectangle 1:100
Area of Rectangle 2:400
Java this keyword Using With Function: Java using this keyword we can also call a function of class without creating an object of that class.
How To Use Java this Keyword With Function?
class Geometry
{
public void rectangle_area(int h,int w)
{
int area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public void square_area(int side)
{
int area=side*side;
System.out.println("Area of Square:"+area);
this.rectangle_area(17, 17);
}
}
class icoderweb
{
public static void main(String[] args)
{
Geometry obj=new Geometry();
obj.square_area(14);
}
}
*****OUTPUT*****
Area of Rectangle:289
Area of Square:196
Java this keyword constructor: Java using this keyword we can also call a function of class without creating an object of that class.
How To Use Java this Keyword With Constructor?
class Rectangle
{
Rectangle(int h,int w)
{
int area=h*w;
System.out.println("Area of rectangle:"+area);
}
public Rectangle()
{
this(17,17);
System.out.println("I am inside Default Constructor");
}
}
class icoderweb
{
public static void main(String[] args)
{
Rectangle obj=new Rectangle();
}
}
*****OUTPUT*****
Area of Rectangle:289
I am inside Default Constructor