简体   繁体   中英

How to call a parent class method using child class reference using super keyword for object creation?

ClassA :

package assignment;

public class A {
public static void m1() {
    System.out.println("m1 method");
}
public void m2() {
    System.out.println("m2 method");
}

}

class B:

package assignment;

public class B extends A{
public static void m1() {
System.out.println("B class m1 method");
}
public void m2() {
System.out.println("B class m2 method");}}

How to use class A static method using class B?Is there any possible solution if any please suggest few ways.

To call the method of the super class, the object should be of Super class. As :

A parent=new A();
parent.m1();

Or

Or you can override the super class function and call the Super.Function()

@Override
public void m1() {
    super.m1();

}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM