CodeGym /Java Course /Java Multithreading /Inheriting nested classes

Inheriting nested classes

Java Multithreading
Level 4 , Lesson 3
Available
Inheriting nested classes - 1

"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:"

Example
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?"

Example
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:"

Code
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."

Comments (14)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Justin Smith Level 41, Greenfield, USA, United States
13 January 2022
Inheriting a nested class from outside the parent class, while possible, might be an indication that the project hasn't been planned out well.
Dawid Level 28
30 March 2021
I would like to explain the syntax thing raised in the comments regarding car.super() call. So when we have super() call in the constructor, it always means that the constructor of the direct superclass will be invoked. So in this case we have constructor of the LamborghiniDoor class therefore Car.Door contructor will be called. Moving on to nested classes we know, an inner class object will not be created without its outer class object so the instance of a enclosing class(Car class) is needed to execute the constructor of inner class(Car.Door). That's why we need to send an instance of Car class to the constructor in LamborghiniDoor the class and therefore we also have this rare syntax ( car.super() ) because by calling the constructor of Car.Door we need to indicate a specific object of the Car class on which this constructor will base it's work.
Andrei Level 41
7 April 2021
Ah, ok, that makes more sense. Great point! Here is a formatted version if you want to update your, to make it more easier to read/understand. I would like to explain the syntax situation raised in the comments regarding car.super() call. When we have super() call in the constructor, it always means that the constructor of the direct superclass will be invoked. So in this case we have constructor of the LamborghiniDoor class therefore Car.Door constructor will be called. Moving on to nested classes we know, an inner class object will not be created without its outer class object so the instance of a enclosing class(Car class) is needed to execute the constructor of inner class(Car.Door). That's why we need to send an instance of Car class to the LamborghiniDoor constructor and therefore we also have this rare syntax ( car.super() ). By calling the constructor of Car.Door we need to indicate a specific object of the Car class on which this constructor will base it's work.
John Squirrels Level 41, San Francisco, Poland
8 April 2021
Hey, Andrei. We sent you the needed links. Please check your DM.
matemate123 Level 50, Kraków, Poland
12 April 2023
Why DM, not here? ...
John Squirrels Level 41, San Francisco, Poland
12 April 2023
Johannes Level 27, Centurion, Pretoria, South-Africa
5 May 2020
why car.super(); ? Car class doesn't have a super in this example ?
Vesa Level 41, Kaliningrad, Russia
11 May 2020
This is the syntax for calling the constructor of the Car.Door class, which is the superclass for the LamborginiDoor class. This is a way to implicitly pass a Car object to the Door constructor.

public static void main(String[] args) {
        new LamborginiDoor(new Car());
}
Car constructor Car.Door constructor LamborginiDoor constructor
Wei Cui Level 39, Newark, United States
8 April 2020
for the second example, there are two way to write, the other way may easier to understand;

public class LamborginiDoor extends Car.Door
{
 LamborginiDoor()
 {
  new car.super(); 
// new car().new door()  door method is non static method so must create new reference to use , so super key word could reference every suplerclass method 
 }
}
Vahan Level 41, Tbilisi, Georgia
31 January 2020
In second example - static nested classes inherit static nested classes in other classes - parent class Lamborgini inherits Doors parent class Car. Is it necessary for inheritance of nested static classes to take place?
John Level 27, Washington DC, USA
23 January 2020
What does car.super() do in the last example? It would seem to call the car's parent class which is implicitly Object. If so, I don't know why super is being used.
Marcin Olech Level 41, Kraków, Poland
12 October 2019
Hmm, i'm a bit confused. When I tried to compile the code from this lesson it says that "Inner classes cannot have static declarations." But in this lesson they do, what I am missing?
Juanf Level 26
2 November 2019
Yup, I think that Lamborgini should be in a separate file, as it is also public, and it is not nested. Nested classes in the example are Door and LamborginiDoor , both not inner, but nested classes (both are static). If you create both files for Car and Lamborgini, each with its nested class, it works perfectly.
Hossein Shams Level 26, Atlanta, United States
15 November 2019
Or you can make everything be inner classes of a bigger outer class, like Solution