James Adams
James Adams

Reputation: 748

Accessing a method defined in the Superclass [Java]

If I define 2 classes; one called Super, which has a method called potato(), and the second one, Sub, which extends Super.

I then declare an object of type Sub;

i.e Sub carrot;

within a third class/main within the same package.

Can I access the potato() method in this third class/main via carrot.potato() ?

Upvotes: 1

Views: 81

Answers (2)

Vincent Beltman
Vincent Beltman

Reputation: 2104

Sub extends Super. That means that sub is a super with some extra details. That means that you can treat sub exactly as a Super object. So carrot.potato() is correct.

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

Since every Child Is-A Parent , you can access that method through Child as well. Assuming it is a public method(as you mentioned in the comments).

If you want to see what I mean in the first comment, have a look here.

Upvotes: 0

Related Questions