0

I have Class1 that contains many private inner anonymous classes like:

ClassAInterface var1=new ClassAInterface {
 .....

When I inherit Class2 from Class1 is it possible not to inherit all these classes but replace with new? So all methods that used these class variables will use only objects in inherited class. Or exists another way? So I would like inherit methods, logic of work, but don't like inherit data structures. Thanks

user845279
  • 2,794
  • 1
  • 20
  • 38
user810430
  • 11,181
  • 15
  • 38
  • 43
  • 1
    Nothing that is private can be inherited. Only protected/public members can be inherited. – Bernard May 03 '12 at 14:45
  • From docs: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. – user810430 May 03 '12 at 14:48
  • http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html – user810430 May 03 '12 at 14:49
  • 2
    From the same document: "A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass." – Bernard May 03 '12 at 14:50
  • How private fields of superclass can be used if they were not inherited and for this reason not created? they are created. In my case - public method uses private fields. I would like substitute with another fields for don't re-write methods. Do you understand the problem? – user810430 May 03 '12 at 14:52
  • You _cannot_ use them directly. You can only use them if the superclass has public or protected methods that access these private fields. Please read the _entire_ document you referenced. – Bernard May 03 '12 at 14:54
  • Please be calm and read my question. I don't asked very simple question as you think. I asked about inherited code but not data structure. I can use spring injections, but may be possible use java language structures – user810430 May 03 '12 at 14:56
  • What you're try to do is possibly an abuse of inheritance and could lead to maintainability issues. If you need to use the same function on different values, then those should be parameters, or injected in the constructor of the object. I'm not sure I see any scenario where you'd need to take the same function and run it on different values, using only inheritance to differentiate said values. – Doug Moscrop May 03 '12 at 15:04

3 Answers3

1

You should use nested class or also called static inner class:

public class Class1{

   // fields and methods for Class1

   protected static MyInnerClassA{

       //some fields methods here for MyInnerClassA
   } 
}

Thus, when you inherit from Class1, you could create new instances of MyInnerClassA because this one is accessible. To put in a nutshell, static keyword in this case allows to get Inner class independent of its parent class.

Mik378
  • 21,881
  • 15
  • 82
  • 180
1

Anything private can never be inherited in a sub class.

cvanes
  • 31
  • 3
0

If you're looking to inherit "methods, logic of work, but not data structures", you need to use interface more and composition rather than inheritance.

Doug Moscrop
  • 4,479
  • 3
  • 26
  • 46