ADVERTISEMENT
ADVERTISEMENT

Java program to create an abstract class with a final method

Learn how to create an abstract class with a final method in Java?
Submitted by Nidhi, on March 27, 2022

Problem Solution:

In this program, we will create an abstract class with a final method. Then we will inherit the abstract class into another class.

Program/Source Code:

The source code to create an abstract class with a final method is given below. The given program is compiled and executed successfully.

// Java program to create an abstract class 
// with a final method

abstract class AbsClass {
  final void MyFun() {
    System.out.println("MyFun() called");
  }
}

class Sample extends AbsClass {

}

public class Main {
  public static void main(String[] args) {
    AbsClass abs = new Sample();
    abs.MyFun();
  }
}

Output:

MyFun() called

Explanation:

In the above program, we created an abstract class AbsClass with a final method. Then we will inherit the AbsClass into the Sample class.

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. Then we called MyFun() method using the reference of AbsClass.

Java Abstract Class Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.