简体   繁体   中英

Java Inheritance: Calling a subclass method in a superclass

I'm very new to java and would like to know whether calling a subclass method in a superclass is possible. And if doing inheritance, where is the proper place to set public static void main .

Superclass

public class User {
    private String name;
    private int age;

    public User() {
        //Constructor
    }

    //Overloaded constructor
    public User(String name, int age) {
        this.name = name; 
        this.age = age;
    }

    public String getName() {
        return this.name;
    }
    public static void main(String []args) {
        User user1 = new Admin("Bill", 18, 2); 

        System.out.println("Hello "+user1.getName()); 
        user1.getLevel();
    }

}

Subclass

public class Admin extends User {

    private int permissionLevel;

    public Admin() {
    //Constructor 
    }

    //Overloading constructor
    public Admin(String name, int age, int permissionLevel) {
        super(name, age); 
        this.permissionLevel = permissionLevel;
    }

    public void getLevel() {
        System.out.println("Hello "+permissionLevel);

    }

}

I'm very new to java and would like to know whether calling a subclass method in a superclass is possible.

A superclass doesn't know anything about their subclasses, therefore, you cannot call a subclass instance method in a super class.

where is the proper place to set public static void main.

I wouldn't recommend putting the main method in the Admin class nor the User class for many factors. Rather create a separate class to encapsulate the main method.

Example:

public class Main{
   public static void main(String []args) {
        User user1 = new Admin("Bill", 18, 2); 

        System.out.println("Hello "+user1.getName()); 
        user1.getLevel();
    }
}

Short answer: No.

Medium answer: Yes, but you have to declare the method in the superclass. Then override it in the subclass. The method body from the subclass will be in invoked when the superclass calls it. In your example, you could just put an empty getLevel method on User.

You could also consider declaring User as an abstract class and declaring the getLevel method as abstract on the User class. That means you don't put any method body in getLevel of the User class but every subclass would have to include one. Meanwhile, User can reference getLevel and use the implementation of its subclass. I think that's the behavior you're going for here.

No , it is not possible to call sub class method inside super class.

Though it is possible to call different implementations of the same method in a client code while you have a variable with a super class type and instantiate it with either super class or sub class objects. It is called polymorphism .

Please, consider the following example:

public class User {
    private String name;
    private int age;
    protected int permissionLevel;

    public User() {
        //Constructor
    }

    //Overloaded constructor
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public void getLevel() {
        System.out.println("Hello "+ permissionLevel);
    }
}

public class Admin extends User {

    public Admin() {
        //Constructor
    }

    //Overloading constructor
    public Admin(String name, int age, int permissionLevel) {
        super(name, age);
        this.permissionLevel = permissionLevel;
    }

    @Override
    public void getLevel() {
        System.out.println("Hello "+permissionLevel);
    }

    public static void main(String []args) {
        User user1 = new Admin("Bill", 18, 2);

        System.out.println("Hello "+user1.getName());
        user1.getLevel(); //call to subclass method

        user1 = new User("John", 22); //the variable is the same but we assign different object to it
        user1.getLevel(); //call to superclass method
    }
}

Answering your second question, no, it does not matter where you place your main method as long as it is of right method signature. As you can see in my example I moved the method to Admin.java - it is still acceptable.

Calling subclass method in a superclass is possible but calling a subclass method on a superclass variable/instance is not possible.

In java all static variable and methods are considered to be outside the class ie they do have access to any instance variable or methods. In your example above it will be wise to create a new class called Main and put public static void main in there but this is just a hygiene issue and what you have above will work except for the line.

user1.getLevel()

Use case: If employee eats, then automatically should sleep:-)

  1. Declare two methods eat and sleep from class person . Invoke the sleep method from eat .

  2. Extend person in the employee class and override only the sleep method:

     Person emp=new Employee(); emp.eat();

Explanation: As eat method is not in subclass, it will invoke the super class eat . From there, sub class's sleep will be invoked.

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