简体   繁体   中英

Why do you need non-abstract class in java?

I've written a Bike interface and implementing MountainBike class as below:

//interface

package com.company;

public interface Bike {

    void changeCadence(int newValue);
    void changeGear(int newValue);
    void speedUp(int increment);
    void applyBrakes(int decrement);

}
//implementing class 

package com.company;


public class MyBike implements Bike{

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    // The compiler will now require that methods
    // changeCadence, changeGear, speedUp, and applyBrakes
    // all be implemented. Compilation will fail if those
    // methods are missing from this class.

    public void changeCadence(int newValue) {
            cadence = newValue;
    }

    public void changeGear(int newValue) {
            gear = newValue;
    }

    public void speedUp(int increment) {
        speed = speed + increment;
    }

    public void applyBrakes(int decrement) {
        speed = speed - decrement;
    }

    void printState(){
        System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear);
    }

    public static void main(String[] args) {
        MyBike mBike = new MyBike();
        mBike.changeCadence(20);
        mBike.changeGear(3);
        mBike.speedUp(20);
        mBike.applyBrakes(20);
        mBike.printState();
    }
}

I understand that in this case I have to implement all the classes in interface within implementing class. However, if I use abstract class, I don't need to implement them all. If that's the case why not always use abstract class, in case I might implement only partially or maybe all classes from interface? Why do we need to use non-abstract class?

An abstract classs can NOT be instantiated by using new operator. Hence you need concrete implemented classes, of which you can make an instance object.

See Why can't we instantiate a abstract class in JAVA? for more details.

Good read Interface vs Abstract Class Interface vs Abstract Class (general OO)

If you have a abstract class if you call the constructor you have to manualy override the missing methods, thats not realy clean and looks a bit sketchy, if you dont use abstract classes you have all code in your class or a super class of that class

Using abstract classes, which implements an interface without actually implementing the methods directly, doesn't absolve you from implementing the interface methods at some point somewhere. The task to implement the method has just be moved to the next class which inherits from your abstract class. It is correct to write

public abstract class MyBike implements Bike {
}

but then the next class has this task to deal with the not implemented methods. In the end, one class in the class inheritance hierarchy have to implement the methods defined by the interface.

Keep in mind, you cannot write new MyBike() when MyBike is declared as static .

To achieve the effect you describe add default methods to the Bike interface for the methods that you do not want the implementing class to have to instantiate and make the MyByke class concrete. I am not saying this will make sense for this particular example, only that it will solve the specific issue.

BTW I suggest using conventional naming: setCadence rather than changeCadence, etc.

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