This is because at runtime, kaylasAccount
is recognized as the CheckingAccount
it is. So, what if CheckingAccount
has a method transferToSavings()
that BankAccount
does not have? Can kaylasAccount
still use that method?
Well, no. The compiler believes that kaylasAccount
is just a BankAccount
that doesn’t have some fancy child class transferToSavings()
method, so it would throw an error.
I’m very confused by this. If kaylasAccount is a CheckingAccount, which is a subclass of BankAccount, and as such is recognized as a CheckingAccount at runtime, then why would it not have all CheckingAccount methods available? Wouldn’t that defeat the purpose of it being a subclass if as a result it loses access to its own method(s)?
This question is not really related to the casting object thing but is related to instance variables instead:
In the class Noodle:
class Noodle {
protected double lengthInCentimeters;
protected double widthInCentimeters;
protected String shape;
protected String ingredients;
protected String texture = "brittle";
Noodle(double lenInCent, double wthInCent, String shp, String ingr) {
this.lengthInCentimeters = lenInCent;
this.widthInCentimeters = wthInCent;
this.shape = shp;
this.ingredients = ingr;
}
public void cook() {
this.texture = "cooked";
}
}
Why is the texture “brittle” not initialized in the constructor?
A more general question is: When I declare variables that are not class variables (without “static”), are they 100% considered instance variables? Will the object have a copy of this variable for their own? Because when we create a new object, eg: Noodle something = new Noodle();, we call the constructor method Noodle(), and inside that constructor we don’t write anything about the variable texture
T
Yes, a variable (that’s not static) declared for the class , but not in a method, would be an instance variable, and every instance/object would have its own copy of that variable.
So doing this:
public class Example {
public int x = 3;
}
is similar to doing
public class Example {
public int x;
public Example() {
this.x = 3;
}
}
In the class Noodle
, this means any new instances of Noodle
will start with having .texture
be "brittle"
even if there is nothing for that in the constructor.
Hi everybody!
I still don’t understand how could we use Noodles class and its child class in Dinner Class. I’ve met the answer below, that we import them by import statement, but I didn’t met this statement in Dinner.java file.
Excuse me, if I wasn’t attentive enough.
Thank you!
Thank you. This really helped me. I didn’t understand the BiangBiang class.
This is a confusing concept to grasp, but its essentially upcasting where myNoodle is treated as a Noodle instance by compiler, whereas during runtime it is actually a BiangBiang instance. So it can use BiangBiang properties that are overridden from the Noodle parent. What’s the use of this?
- Improves code reusability: you can use methods that work with Noodle class, i.e. makeNoodle, along with its child BiangBiang as well. You don’t need to write another method makeNoodle method to work specifically for an object BiangBiang type. Really cool!
- Improves flexibility: by just focusing on coding with superclasses rather than it’s child classes, you can easily modify functionality of coding with superclass, without being concerned of child classes. You can just focus on the functionality of Noodle class, without thinking about how to implement the same functionality for BiangBiang.
- With code reusability and flexibility, you can have a more efficient and maintainable codebase. OOPs is awesome!!