PhysicsTeacher.in

High School Physics

Java Program with Abstract classes

Last updated on February 5th, 2022 at 02:56 pm

Here is a Java program that helps you to understand the concepts of Abstract classes. Type, compile and run to understand how to use the concepts of Abstract class in a Java program.

Java program to understand the concepts of Abstract class

//a class can be declared abstract even if it does not have any abstract met

abstract class AbsClass
{
}

// must declare a class abstract if it has an abstract method

abstract class AbsClass2
{

/* must declare a method as abstract if
the method has no method body */

abstract void absMeth();
}

/*must declare a class as abstract if it does not override
the abstract method of its base class */

abstract class Child extends AbsClass2
{
int i,j;
Child(int i,int j)
{
this.i=i;
this.j=j;
}
}

//proper implementation of abstract methods
class Child2 extends Child
{
int k;
Child2(int i,int j,int k)
{super(i,j);
this.k=k;

}
void absMeth()
{System.out.println(“absMeth implemented in Child2”);
System.out.println(“I,J and K are:”+ i +” “+ j +” “+ k);
}
}

class AbsTest
{
public static void main(String args[])
{
Child2 c2=new Child2(10,12,14);
c2.absMeth();
}
}

Summary: Abstract class in Java

  • A java class can be declared abstract even if it does not have any abstract method.
  • Must declare a class abstract if it has an abstract method.
  • Must declare a method as abstract if the method has no method body.
  • you must declare a class as abstract if it does not override the abstract method of its base class.

Note: if you find any of the features mentioned above is not as per the current Java versions, then please mail us with suggestions for corrections and references.

See also  Java Program to find the greatest and smallest numbers
Scroll to top
error: physicsTeacher.in