public class Home {
public void getHomeDetails(){
Rooms r = new Rooms();
r.getRooms();
}
public class Rooms{
public void getRooms(){
System.out.println("20");
}
}
}
public class BigHome extends Home{
public class Inner extends Home.Rooms{
public void getRooms(){
System.out.println("100");
}
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigHome bigHome = new BigHome();
bigHome.getHomeDetails(); // output:20 (Expecting 100)
}
}
I have a class Home and its inner class Room which prints 20. Another class named BigHome extends Home and its inner class. But when I initiate BigHome in Main Class. I get output as 20 (I was expecting output as 100)
How do I call function getRoom of the BigHome Class ?
BigHome does not have a bigHome.getHomeDetails(); method, so it uses the method in the Home class which uses home's rooms. What you can do is setup constructors for the 2 classes, and in these constructors, then setup which Room class you want to use. Like:
public class Home
{
Rooms r;
public Home()
{
r = new Rooms();
}
public void getHomeDetails()
{
r.getRooms();
}
public class Rooms
{
public void getRooms()
{
System.out.println("20");
}
}
}
class BigHome extends Home
{
public BigHome()
{
r = new Inner();
}
public class Inner extends Home.Rooms
{
public void getRooms()
{
System.out.println("100");
}
}
}
Now Home and BigHome are both in charge of specifying which Rooms they want to use.
Another approach, which takes away from the whole concept of inheritance, is to override the getHomeDetails method in the BigHome class to set which rooms BigHome is using.
public void getHomeDetails()
{
Inner.Rooms r = new Inner.Rooms();
r.getRooms();
}
I would not advise on this though.
You could do this by moving the construction of a new Rooms
instance into another method in Home
, which you would then override in BigHome
to return a new Inner
instance instead:
public class Home {
public Rooms createRooms() {
return new Rooms();
}
public void getHomeDetails(){
Rooms r = createRooms();
r.getRooms();
}
...
}
public class BigHome extends Home{
@Override
public Rooms createRooms() {
return new Inner();
}
...
}
You define the method getHomeDetails()
as
public void getHomeDetails(){
Rooms r = new Rooms();
r.getRooms();
}
and you never override this in any subclass. As such it never changes that you call getRooms()
on a Room
object and it will therefor always print 20.
That BigHome
contains another class which also implements a getRooms()
method is irrelevant. That this new class is also called Rooms doesn't mean it's the same class. When you call getHomeDetails()
only the first Room
object nested within Home
will be instantiated and getRooms()
will be called on this object and not any other.
BigHome
does not have a getRoom
method, there is a getRooms
in Rooms
and Inner
.
If you create an instance of BigHome.Inner
and call .getRooms()
, it will call BigHome.Inner.getRooms
. If you create an instance of Home.Rooms
and call .getRooms()
, it will call Home.Rooms.getRooms
.
The problem you are facing is that getHomeDetails
is defined in the parent class, Home
and not anywhere else. It creates an instance of Home.Rooms
because that is what the code says to do and thus the 20.
Try over-writing getHomeDetails
in Inner
:
public void getHomeDetails(){
Inner.Rooms r = new Inner.Rooms();
r.getRooms();
}
That should print "100" instead of "20".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.