6
votes

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);

    }

}
5
No, it's no possible. A class has no way to know if or when it's been subclassed. A User is not always an Administrator, but an Administrator IS-A User. Administrator can call parent methods, but not the other way around, unless you cast a User reference to an Administrator.duffymo
I’ve made a habit of placing the main method outside of all “functional” classes. In your example, I’d create a class Functionality which holds the main method, and your two extra classes. What if you program may one day provide functionality which has nothing to do with users. The simplest use case may be displaying a help text – that wouldn’t fit in the User class, could it?bleistift2
Just from a glance, it appears that in order to call user1.getLevel() you will either need to declare user1 like Admin user1 or cast user1 like so ((Admin) user1).getLevel().Brendon Boldt

5 Answers

5
votes

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.

5
votes

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();
    }
}
2
votes

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.

0
votes

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 i.e 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()
0
votes

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.