inner class extending

inner class inheritance java
java inner class
nested class and inheritance
can we extend static class in java
java outer class
static inner class
when to use nested classes
how to call inner class method in java

In java, say I have the following class:

public class A{
  protected class B{
  }
}

can I extend the inner class by doing the following?

public class C extends A{
  protected class D extends B{
  }
}

What I want to do is that I have the class C above and I need to change something in A's inner class so I was thinking that I need to extend the inner class to do so but I wasn't sure how exactly to do that.

According to this page, you have the right way figured out to extend inner classes. A few tips about it can be found here, in the middle of the article (search for "extend").

Java: Extending inner classes, You can extend static inner class with another inner class. Example. Live Demo public class SampleClass { static  When you instantiate Slice class, it gives call to the super class and invokes sliceGenerator. This method internally creates instances of Pie.Slice and not ApplePie.Slice. To get around this, make sliceGenerator method protected and override it in Apple.Slice class.

If that doesn't work (I don't think it will), the following should:

public class C extends A{
  protected class D extends A.B{
  }
}

Well, it will, but what if you have another class named B outside of A?

Edit: nope, it makes no difference. Even if there is another class named B, it will still take A.B. So your syntax is correct.

Can you extend a static inner class in Java?, Is it a private or public inner class? Do you want to extend it inside the class or in another class? Why would you want to? What have ou tried so  The EvenIterator inner class, which implements the DataStructureIterator interface, which extends the Iterator< Integer> interface. Iterators are used to step through a data structure and typically have methods to test for the last element, retrieve the current element, and move to the next element.

You are allowed to do that but don't expect your C class to use D class instead of A's inner B class automatically.

How to extend inner class (Java in General forum at Coderanch), A regular class can extend a class and implement any number of interface simultaneously. But anonymous Inner class can extend a class or can implement an  Inner class means one class which is a member of another class. There are basically four types of inner classes in java. Nested Inner class can access any private instance variable of outer class. Like any other instance variable, we can have access modifier private, protected, public and default modifier.

Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.

--- Thinking In Java

Anonymous Inner Class in Java, Nested Inner class can access any private instance variable of outer class. Like any It can either extend a class or implement interface at a time. This article is  Java inner class or nested class is a class which is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. Additionally, it can access all the members of outer class including private data members and methods. Syntax of Inner class

Inner class in java, A nested class is a member of its enclosing class. Non-static nested classes (​inner classes) have access to other members of the enclosing class, even if they are  True inner classes are rare enough, and their use tends to suggest finality, not extension. If it's for the SCJP exam, then good luck with that; but the chances of you ever needing to use it in real life are slim to none (in eleven years of Java programming, I haven't).

Nested Classes (The Java™ Tutorials > Learning the Java , Anonymous classes are inner classes with no name. Since they have no We may either extend an existing class or implement an interface. A normal class can implement any number of interfaces but anonymous inner class can implement only one interface at a time. A regular class can extend a class and implement any number of interface simultaneously. But anonymous Inner class can extend a class or can implement an interface but not both at a time.

Anonymous Classes in Java, Interfaces and inner classes provide more sophisticated ways to organize You can see that InheritInner is extending only the inner class, not the outer one. Absolutely. If your class requires the use of a number of other classes that are related to each other (ie form a class hierarchy) and those classes are particular to the parent class (ie are ideal candidates for implementation as inner classes) then it is probable that the super class of the inner class hierarchy will be abstract.

Comments
  • Yes you can extend the inner class like that.
  • It wouldn't work if D was static, but it's not, so it does.
  • This is the truly crucial aspect with inheritance of outer/inner class data structures. Is there any known pattern to deal with this problem (generics etc.)?
  • I'd say inject the actual instance of B (B or D in this case) you want to use.