I have the below two classes. I'm wondering how I'm able to call the instance method of ClassA
ie AMessage()
in class B with out the instance of a ClassA
or ClassB
created?
I was thinking I should call instance method of ClassA
ie AMessage()
in class B as below:
new ClassA().AMessage(); //no compile error
new ClassB().Amessage(); //no compile error
Parent Class (ClassA.java)
public class ClassA {
public void AMessage(){
System.out.println("A Message");
}
}
Child Class (ClassB.java)
public class ClassB extends ClassA{
public void BMessage(){
AMessage(); //no compile error
}
public static void main(String[] args){
new ClassB().BMessage();
}
}
Use super
keyword to call base class methods. Check this for reference - http://docs.oracle.com/javase/tutorial/java/IandI/super.html
public class ChildClass extends BaseClass{
public static void main(String[] args){
ChildClass obj = new ChildClass();
obj.Message();
}
public void Message(){
super.Message();
}
}
class BaseClass {
public void Message(){
System.out.println("Base Class called");
}
}
Or you can do it like this as well -
public class ChildClass extends BaseClass{
public static void main(String[] args){
new ChildClass().Message();
}
}
class BaseClass {
public void Message(){
System.out.println("Base Class called");
}
}
yes you can call method directly instance method of parent class from child class object provided parent class method should be public
you can call in this way
new ClassB().AMessage();
it will also work
When a class extends another class, it automatically inherits the visible fields and methods of the base class. By visible I mean accessible members. Private members are not inherited. Learn more about inheritance in http://www.tutorialspoint.com/java/java_inheritance.htm
So if you have a class ClassA having a method AMessage like this :
public class ClassA{
public void AMessage(){
System.out.println("A message");
}
}
and ClassB which extends ClassA like this :
public class ClassB extends ClassA{
public void BMessage(){
AMessage();
}
public static void main(String[] args){
new ClassB().BMessage();
}
}
ClassB automatically inherits members of ClassA ie they act as if they are members of ClassB itself. That's why we can call the instance method of ClassA inside ClassB without any instance, because they belong to ClassB as well. Also besides instance methods, you can call static methods like that as well. (But of course you can not call instance methods inside static methods.)
As an additional answer I would like to suggest that (although it is not related to the question but it is a good practice) you should not name any method of a class (instance or static) starting with a capital letter. It doesn't generate any compiler error if you still name it like that, but it affects the readability. I have written the names like that only to relate to your question.
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.