-1

Lets say I have a class called Person. Then I make a child class called Adult (so an adult would extend Person) Lastly, I make a child class of the Adult class called Teacher (so the Teacher would extend Adult)

If there was a toString method (@Override) for Person class, does all the child class (Teacher and Adult) have to have a toString method?

And if there is a toString method specific to Person and another toString specific to Adult class, how can I reference the toString method from the Teacher class from the Adult class using super reference? (all toStrings are overridden)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Joey Kim
  • 9
  • 4
  • 4
    having student extend teacher makes zero sense. it's saying all students are teachers. – Nathan Hughes Mar 13 '16 at 05:13
  • 1
    *"does all the child class (teacher and Student) have to have a toString method?"* - `toString` is inherited from `Object`, so (technically speaking), they all have that method. If `Person` overrides `toString`, then when you call `Teacher#toString` or `Student#toString`, they will make use of `Person#toString`. You only need to override it if you wish to change what `Person#toString` changes – MadProgrammer Mar 13 '16 at 05:15
  • @cricket_007 *"so the Student would extend Person"* - I think it's typo/misunderstanding of the OP - one way or another, it's confusing – MadProgrammer Mar 13 '16 at 05:16
  • ["Is-a relationship"](https://en.m.wikipedia.org/wiki/Is-a) (in reference to what Nathan is saying) – Vince Mar 13 '16 at 05:17
  • Do I understand this correctly? You want to write code in the `Adult` class that calls the `Teacher` class's version of `toString`? If so, then this actually happens when you call `toString` if the object you're calling it on is a `Teacher`. And if it isn't a `Teacher`, then it wouldn't make sense to call the `Teacher` class's method, since that method might reference methods (like `throwChalk`) that only exist in the `Teacher` class. – Dawood ibn Kareem Mar 13 '16 at 05:34

2 Answers2

0

I'm going to simplify your question by renaming the classes A, B, and C. B extends A. C extends B.

For your first question: If A has a public doSomething method, then because of inheritance, B and C also have a doSomething method. They don't have to declare or implement it, but they may if they wish to override the functionality and the method is not final.

For your second, you cannot reference the super of your super. For instance, C could not call super.super.doSomething expecting to reference the doSomething in A, bypassing the doSomething in B. By choosing to extend B, you take all the warts along with it, including whatever it chooses to do in its doSomething implementation. If you need to reference A's doSomething from within C, then perhaps you extended the wrong class. There are some great answers to that question here: Why is super.super.method(); not allowed in Java?

Community
  • 1
  • 1
digitaljoel
  • 26,265
  • 15
  • 89
  • 115
0

If there was a toString method (override) for Person class, does all the child class (teacher and Adult) have to have a toString method?

By the inheritance, all the subclasses(child) of Person automatically have a toString method, but return a string of Person (the parent) no the current class (the child).

if Employe extends Person, then you can call employe.toString() and will return the implemented on the Person class.

Sergio Inxunxa
  • 283
  • 3
  • 6