abstractclass_staticmethod1.htm
 
  • An abstract class may have static fields and static methods
 

Calling static method in abstract class

Complete Code:

/**
*
*/
import java.io.*;
import javax.swing.JOptionPane;
import java.net.URI;
/**
* @author Manas9
*
*/
public class ClassTemplate1 {
/**
*
*/
public ClassTemplate1() {
// TODO Auto-generated constructor stub

}
public static void main(String[] args)
throws Exception {
// TODO Auto-generated method stub
//myClass mls = new myClass();
// can't instantiate abstract class
try {
A a1 = new A();
B b1 = new B();
a1.rememberMe();b1.rememberMe();
myClass.companion_method();
//
} catch (NumberFormatException exc) {
//
}
}

}// end of class template

abstract class myClass {
//abstract method must be empty
abstract void rememberMe();
// default method
static void companion_method()
{
System.out.println("This is a regular method");
}
}
class A extends myClass{
void rememberMe(){ //subclass must define it
System.out.println("must have in the subclass");}
}
class B extends myClass{
void rememberMe()
{ System.out.println("must have in the subclass");}
//myClass m1 = new myClass(); // not allowed
//
}

Runtime Views: