Reputation:
I feel I have a decent understanding of inheritance in Java but am struggling with the following situation:
public class SuperClass {
private String name;
private int age;
public SuperClass(String name, int age) {
this.name = name;
this.age = age;
}
public void sharedMethod() {
System.out.println("I'm a shared method in the SuperClass");
}
}
public class SubClass extends SuperClass {
public SubClass(String name, int age) {
super(name, age);
}
public void sharedMethod() {
System.out.println("I'm a shared method in the SubClass");
}
public void subMethod() {
System.out.println("I'm subMethod");
}
}
public class Main {
public static void main(String args[]) {
SuperClass test = new SubClass("Laura", 30);
test.sharedMethod();
test.subMethod(); // causes an error
}
}
I am primarily struggling to understand what is actually being created here when using SuperClass test = new SubClass("Laura", 30);
(which only seems to work when SubClass
extends SuperClass
- how's that possible without Casting?).
The reason I don't understand it is because I can call the sharedMethod
which prints out the text from the SubClass
method (so presumably SubClass
is overriding the SuperClass
method in this instance), but yet I can't call the subMethod
from the SubClass
at all. Sorry if I didn't articulate it very well, I just can't seem to wrap my head around what is being referenced, what methods can be used, why you would do this, why the SubClass
overrides a method if the method is shared, but can't be called otherwise.
Upvotes: 1
Views: 93
Reputation:
Let's say you had a you told somebody you had a vehicle:
class Vehicle {
public void drive() {
System.out.println("Driving vehicle");
}
}
That means, the person you told knows you are capable of driving...But, then you got a motorcycle:
class Motorcycle extends Vehicle {
public void wheely() {
System.out.println("Doing a wheely");
}
public void drive() {
System.out.println("Driving motorcycle");
}
}
Which can drive()
because its a Vehicle
, and also do a wheely()
. But, you never told them the type of vehicle you had was a motorcycle. So, the most they know you are capable of doing is driving.
The level of knowledge that the person has is similar to the object reference:
Vehicle vehicle = new Motorcycle();
The Vehicle
reference only knows you are a Vehicle
. It doesn't matter "HOW" you drive, it just knows that you know how to drive. So, when you call drive()
it will drive how Motorcycle
drives (overridden method), but you are still driving.
But, if you were to be more SPECIFIC and tell someone the type of vehicle you had was a motorcycle, you are giving them MORE information about the CAPABILITIES of your vehicle.
Motorcycle motorcycle = new Motorcycle();
You know since it's a Vehicle
it still can do all that a vehicle can do drive()
. But, since you now have a reference to the subclass, you know more about what it can do - wheely()
.
The compiler cannot make assumptions, so if you make a reference Vehicle v
that is LESS specific (a super class) the compiler will only access elements of that object that it KNOWS for sure it has access to. If you make a MORE specific reference (a sub class), then you are telling the compiler more about what that object can do.
Upvotes: 1
Reputation: 28279
By SuperClass test = new SubClass("Laura", 30)
, you create a SubClass
instance, but reference it by SuperClass
.
At compile stage, the compiler found that SuperClass
does not have method subMethod
, so you get compile error.
At runtime, JVM found that test
is actually a SubClass
, so the sharedMethod
from SubClass
:
public void sharedMethod() {
System.out.println("I'm a shared method in the SubClass");
}
will be executed.
Upvotes: 2