Tags

, , ,

https://leanpub.com/u/sanjibsinha

A class can be extended and at the same time, it may implement multiple interfaces. The next problem is a showcase for the same example.

package fun.sanjibsinha;
/*
class cannot extend multiple classes
so we cannot write like this: public class CatyClass extends MobileClass, MobileFeatures
 */
public class CatyClass extends MobileClass implements MobileUpgradable {

    private double batteryStrength = 0.0;
    private int pixel = 10;
    private double mins = 60.0;
    private int days = 0;
    public double version = 1.0;

    public double getVersion() {
        return version;
    }

    public void setVersion(double version) {
        this.version = version;
    }

    public double getBatteryStrength() {
        return batteryStrength;
    }
    public void setBatteryStrength(double batteryStrength) {
        this.batteryStrength = batteryStrength;
    }

    public double getMins() {
        return mins;
    }
    public void setMins(double mins) {
        this.mins = mins;
    }

    public double takeCharge(double mins){
        this.mins = mins;
        return mins;
    }
    public double batteryCanLast(int days){
        this.days = days;
        return days;
    }

    public CatyClass(){}
    public CatyClass(String name, String color, double price, double mins){
        super(name, color, price);
        this.mins = mins;
    }

}

After the class declaration, we need an interface.

package fun.sanjibsinha;

interface MobileUpgradable {

    double getVersion();
    void setVersion(double version);
}

And finally, we need the main application.

package fun.sanjibsinha;

public class ProblemNinetySeven {

    public static void main(String[] args) {
        CatyClass catyOne = new CatyClass();
        catyOne.name = "New Caty";
        catyOne.setHeight(8.12);
        System.out.println(catyOne.name + " has height of " + catyOne.getHeight() + " inches.");
        System.out.println("The New Zeny mobile object has unique identifier: " + catyOne.hashCode());
        System.out.println(catyOne.name + " represents the " + catyOne.getClass());
        System.out.println();
        catyOne.version = 2.0;
        System.out.println(catyOne.name + " has version " + catyOne.version);
        catyOne.setVersion(3.0);
        System.out.println(catyOne.name + " is upgraded to " + catyOne.getVersion());
    }
}

Through the contract ‘MobileUpgradable’ now the new mobile object can implement the new version. Exactly that has been done. Watch the output, and you will get the idea.

//output
New Caty has height of 8.12 inches.
The New Zeny mobile object has unique identifier: 1720435669
New Caty represents the class fun.sanjibsinha.CatyClass

New Caty has version 2.0
New Caty is upgraded to 3.0

By default the interface methods are ‘public’ and ‘abstract’. So we don’t have to mention it when we declare a method in the interface. The next problem is the showcase of the discussion we have had earlier. I hope this example will give you more clear insights about this contract is being implemented.