I want to know if it possible to use This() To reffer MotherClass, Calling My motherClass A and executing it from my Child Class B. witch one is correct ?
Class A { A(){ System.out.println("hello");}
Class B extends A { this() ; B(){System.out.println("World");}}
OR
class A {
Class B { this(); B(){System.out.println("World");} }
}
I want when to Call Class B it show Me (HelloWorld);
Sorry for my bad english.
Calling this()
is meaningless unless you are calling it in a constructor and which means to call a constructor overload for this current class (if one exists). Please see this tutorial for the details on this. To call a super class's methods, again use the super
keyword.
Incidentally, your code still doesn't compile. Please understand that you can't be careless when coding -- the compiler won't let you, and neither should you be when creating and posting code for questions here.
public class parent {
parent(){
System.out.println("Hello");
}
}
public class child extends parent {
child(){
super();
System.out.println("World");
}
public static void main(String[] args) {
child c = new child();
}
}
==============================
Creating an instance of child class will first call the parent class constructor and then the child class constructor; resulting in the o/p you are looking for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.