Is it possible to compile java class child of parent class, that invoking method of child class does't requre load of parent class.
May be compile or run with some advanced option.
For example I have 3 classes
public class Parent {
public void baseMethod(){
System.out.println("Parent.baseMethod invoked");
}
}
public class Child extends Parent{
public void childMethod(){
System.out.println("Child.childMethod invoked");
}
}
public class Main {
public static void main(String[] args) {
new Child().childMethod();
}
}
I compile them, and delete Parent.class.
I want to run
java Main
without exeption
No. Way. Java will always load the superclasses. The superclass is required to create the actual instance.
Creating a new Child
will do an implicit call to Parent()
and that's when you need the Parent
class loaded.
PS - doesn't even work if you just want to call a static method on the subclass, like that:
public class Parent {}
public class Child {public static void hello(){System.out.println("hello");}}
public class Caller{public static void main(String[] args){Child.hello();}}
deleting Parent.class
will throw a NoClassDefFoundError
even though no instance of Child
was ever created.