Q:

Java program to create a simple abstract class

belongs to collection: Java Abstract Class Programs

0

Java program to create a simple abstract class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create an abstract class with an abstract method. Then we will implement the abstract method in a class by inheriting the abstract class.

Program/Source Code:

The source code to create a simple abstract class is given below. The given program is compiled and executed successfully.

// Java program to create a 
// simple abstract class

abstract class AbsClass {
  abstract void MyFun();
}

class Sample extends AbsClass {
  void MyFun() {
    System.out.println("Sample: MyFun() called");
  }
}

public class Main {
  public static void main(String[] args) {
    //Below statement will generate error because,
    //We cannot intentiate the object of AbsClass.
    //AbsClass abs = new AbsClass();
    AbsClass abs = new Sample();
    abs.MyFun();
  }
}

Output:

Sample: MyFun() called

Explanation:

In the above program, we created an abstract class AbsClass with an abstract method. Then we will inherit the AbsClass into Sample and implemented the MyFun() method.

The Main class contains a method main(). The main() method is the entry point for the program, here we created a reference of AbsClass and initialized it with the reference of Sample class and called MyFun() method.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to create an abstract class with the ... >>