Here I make two classes A And prac2 and I just want to print the method from the both of class . But I got the error type of Class A is already defined . So I can't undestand why the error comes
package revision;
public class Prac2 extends A {
public void m() {
System.out.println("child");
}
public static void main(String[] args) {
A obj1 = new Prac2() ;
Prac2 obj2 = new Prac2() ;
obj1.m();
obj2.m();
}
}
class A {
public void m() {
System.out.println("parent") ;
}
}
Exception in thread "main" java.lang.NoSuchMethodError: 'void revision.A.m()'
at revision.Prac2.main(Prac2.java:13)
The type A is already defined
your code seems to be ok with no exception, but it will print:
child
child
you extends Prac2
from A
, that means A
is the super class and if you want to get access to both methods, you should create instances from A
:
class Prac2 extends A {
public void m() {
System.out.println("child");
}
public static void main(String[] args) {
A obj1 = new A() ;
A obj2 = new Prac2() ;
obj1.m();
obj2.m();
}
}
class A {
public void m() {
System.out.println("parent") ;
}
}
the output will be:
parent
child