Home > database >  how to work java instance type in mismatch return type(ex, Object) and reference type(ex, MountBike)
how to work java instance type in mismatch return type(ex, Object) and reference type(ex, MountBike)

Time:11-21

public class Bicycle {

    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;

    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }

    public void printDescription(){
        System.out.println("\nBike is "   "in gear "   this.gear
                  " with a cadence of "   this.cadence  
                " and travelling at a speed of "   this.speed   ". ");
    }

}
public class MountainBike extends Bicycle {
    private String suspension;

    public MountainBike(
            int startCadence,
            int startSpeed,
            int startGear,
            String suspensionType){
        super(startCadence,
                startSpeed,
                startGear);
        this.setSuspension(suspensionType);
    }

    public void setSuspension(String suspensionType) {
        this.suspension = suspensionType;
    }

    public void printDescription() {
        super.printDescription();
        System.out.println("The "   "MountainBike has a"  
                getSuspension()   " suspension.");
    }
}
public class Main {

    public static void main(String args[]){
        Object obj = new MountainBike(1,2,3,"soft");
        System.out.println(obj.getClass()); // ① Q1
        System.out.println(obj.getClass().getSimpleName());

        obj.printDescription(); // ② Q2
        ((Bicycle) obj).printDescription(); // ③ Q3

    }
}

Console Result. (Q1, Q3)

class MountainBike

MountainBike

Bike is in gear 3 with a cadence of 1 and travelling at a speed of 2.

The MountainBike has asoft suspension.

Compiler Error. (Q2)

Cannot resolve method 'printDescription' in 'Object'

① Question 1 'obj' instance was expected to be created and operated as Object Type, but the result of 'getClass()' was output as 'MountainBike' Class.

② Question 2 Also, if it operates as 'MountainBike' Type, the 'obj.printDescription' command should work, but the compiler could not find the 'printDescription' method because it was operated as an Object Type.

③ Question 3 So, by type casting 'obj' to 'Bicycle' , the output of 'Bicycle's printDescription' was expected, but 'MountainBike's printDescription' was output.

I would appreciate it if you could point out the lack of knowledge in this regard.

Or is there a site where I can check related materials?

Thanks for your help.

CodePudding user response:

I'm gonna try to keep it simple

1: You create the Mountain Bike as a Mountain bike Class object, by assigning it to Object obj you refer to it as an Object, all classes in Java extend object, so it is ok to for you to do this. "It doesn't lose" the properties of Mountain Bike, because it remains so, but you can't access them unless you cast it back.

2: By choosing to assign to Object you can only operate it as an object, meaning that you choose to only retain the basic methods like toString() and equals(), so you cannot invoke the methods of MountainBike, but as said before the object is still a MountainBike, you can cast it back.

3: By casting the object to Bicycle you can now access the methods and fields of bicycles, but what you did in Mountain Bike class is called an Override, the method of the child class always takes priority on the method of a parent class (hence the name override), you should always annotate an override method with @Override like so:

@Override
public void method(){
    // ...
}

to make it more obvious, it doesn't do anything but it tells the reader it's an implementation override of the parent class or interface.

--- From here on it is just personal opinion ---

Those are some of the basic concepts of OOP, you could start with something simple like W3 schools and then you can try checking out the Derek Banas Youtube Channel for patterns, they're explained pretty well and I find his way of explaining very clear. But in the end nothing is better than just starting to work with it though, try making a small table game for example and search for answers for things you don't know along the way, at the end you'll have learned more than following any course.

  • Related