"Hi, Amigo!"
"Hi, Kim."
"I want to tell you about inheriting static and non-static nested classes."
"I'm ready."
"There really aren't any issues with inheriting static nested classes. They are inherited just like regular classes:"
public class Car
{
public static class Door
{
}
}
public class LamborghiniDoor extends Car.Door
{
}
"But can we make static nested classes inherit static nested classes in other classes?"
"Why not?"
public class Car
{
public static class Door
{
}
}
public class Lamborghini extends Car
{
public static class LamborghiniDoor extends Car.Door
{
}
}
"OK, got it. They are inherited just like regular classes, right?"
"Yes. But non-static nested classes (known as inner classes) are not inherited as easily."
"When an instance of an inner class is created, a reference to its outer class is stored and implicitly passed to the constructor."
"As a result, when you create objects of a class that inherits an inner class, you must pass the required outer object explicitly."
"This is how it looks:"
public class Car
{
public class Door
{
}
}
public class LamborghiniDoor extends Car.Door
{
LamborghiniDoor(Car car)
{
car.super();
}
}
"You must implicitly pass a Car object to the Door constructor. This is done using a special construct: «car.super()»."
"By the way, if you try to create the LamborghiniDoor constructor without any parameters, the program simply won't compile. A little strange, huh?"
"Yeah, there are a couple of nuances, but it's not rocket science."
GO TO FULL VERSION