Quantcast
Channel: Java this in parent and child not working as expected - Stack Overflow
Browsing all 4 articles
Browse latest View live

Answer by midor for Java this in parent and child not working as expected

The way you would set the s-member in the parent-portion of a child-class, would be to offer a protected constructor (it could, of course, also be public): protected Parent(String s) { this.s = s; }...

View Article



Answer by Nadim Baraky for Java this in parent and child not working as expected

You should create a getter method i.e. getS() in the Child class to retrieve "Child" when you call child.getS() in your main method. In this way, you override the getS() method that was inherited from...

View Article

Answer by Prasad Kharkar for Java this in parent and child not working as...

Your getS() methods from parent class is inherited in Child class and hence it is available for Child object. Overriding is only for methods and not for instance variables. So even if you define a new...

View Article

Java this in parent and child not working as expected

I'm misunderstanding something really basic with inheritance. I have a parent class: public class Parent { String s = "Parent"; Parent () {} String getS() { return this.s; } } and a child class:...

View Article
Browsing all 4 articles
Browse latest View live




Latest Images