Utkarsh Saraf
Utkarsh Saraf

Reputation: 495

Superclass and subclass arguments

I am providing two classes as follows:

Appliance class

public class Appliance {
    void start(Appliance t){
        System.out.println("Start Appliance");
    }
}

Toaster Class

public class Toaster extends Appliance {
    void start(Toaster t){
        System.out.println("Start Toaster");
    }
}

RunAppliance Class

public class RunAppliance {
    public static void main(String[] args) {
        Appliance appliance = new Toaster();
        Toaster toaster = new Toaster();
        appliance.start(toaster);
    }
}

Being a newbie,it confuses me between method overloading and overriding and also how arguments get impacted in one argument type is subclass of another argument type.Thus I am presenting 6 conditions for the same:

1)Appliance Class : void start(Appliance t) ; Toaster Class : void start(Appliance t)

2)Appliance Class : void start(Toaster t) ; Toaster Class : void start(Toaster t)

3)Appliance Class : void start(Appliance t) ; Toaster Class : void start(Toaster t)

4)Appliance Class : void start(Toaster t) ; Toaster Class : void start(Appliance t))

5)Appliance Class : void start(Appliance t) & void start(Toaster t) ; Toaster Class : void start(Toaster t)

6)Appliance Class : void start(Appliance t) ; Toaster Class : void start(Appliance t) & void start(Toaster t))

Can anybody suggest me a necessary rule for the same.

Upvotes: 0

Views: 81

Answers (2)

downeyt
downeyt

Reputation: 1306

I think you are asking what will be the output in each of the cases below:

    Appliance appliance = new Appliance();
    Appliance applianceToaster = new Toaster();
    Toaster toaster = new Toaster();
    appliance.start(appliance);
    appliance.start(toaster);
    appliance.start(applianceToaster);
    toaster.start(appliance);
    toaster.start(toaster);
    toaster.start(applianceToaster);
    applianceToaster.start(appliance);
    applianceToaster.start(toaster);
    applianceToaster.start(applianceToaster);

As @duffymo points out, change Toaster to Appliance in the Toaster start method. The start in Toaster will hide the start in Appliance for any object that is a Toaster at runtime. You would need to user super to access the start in Appliance from the start in Toaster.

At compile time appliance looks like Appliance, at runtime it is an Appliance, so the start in Appliance is called, regardless of which parameter is sent to it.

At compile time toaster looks like Toaster, at runtime it is a Toaster, so the start in Toaster is called, regardless of which parameter is sent to it.

At compile time applianceToaster looks like Appliance, at runtime it is a Toaster, so a polymorphic call is made to start in Toaster, regardless of the parameter sent to it.

Upvotes: 0

duffymo
duffymo

Reputation: 308743

Your Toaster class is incorrect. The argument for its start() method should be Appliance, not Toaster.

Your implementation fails the Liskov Substitution Principle. You can't use a Toaster any place an Appliance is called for, because it doesn't override the start() method as written.

Do it this way:

public class Toaster extends Appliance {
    void start(Appliance a){
        System.out.println("Start Toaster");
    }
}

Upvotes: 1

Related Questions