• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Practical/real world situations where inner classes are useful?

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we need inner classes ? Can anyone give me a practical/real world situation where they could be useful ?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't look up the definition of useful, but I imagine it's broad enough to cover most occasions when a thing contributes to the accomplishment of an objective desired by the user. Inner classes can be used, they don't have to be, to accomplish the programmer's objectives, so they're useful. Further, the programmer wouldn't use inner classes if they weren't 'useful.' You either need to define what you mean by useful or accept the fact that they just are.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Sudip Bose wrote:Why do we need inner classes ? Can anyone give me a practical/real world situation where they could be useful ?



Used in building GUI applications using Swing- Adding event handlers.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please see this post.

https://coderanch.com/t/533292/java/java/Collections-sort-guidance-code#2419460
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there can be a lot of examples ... for suppose

you have two classes

1. Animal
2. Dog

Now let's say Dog is your core class that you have to work with ... but just for some requirements you need some generic functionality and you do not want to put that in the Dog class ...

here you go ... you can make another class Animal (inner class) and write your generic code in it ...


and access it from within the Dog class itself ...
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:there can be a lot of examples ... for suppose

you have two classes

1. Animal
2. Dog

Now let's say Dog is your core class that you have to work with ... but just for some requirements you need some generic functionality and you do not want to put that in the Dog class ...

here you go ... you can make another class Animal (inner class) and write your generic code in it ...


and access it from within the Dog class itself ...



Not an example for inner class. Its - Super class- Sub class relationship.
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:

Not an example for inner class. Its - Super class- Sub class relationship.




brother i have explicitly written (inner class) please check ...

Animal and Dog doesn't always need to be declared as a superclass subclass relationship ...
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:but just for some requirements you need some generic functionality and you do not want to put that in the Dog class



You say its a generic functionality and want to create an Inner class for that? How would other classes make use of the Generic functionality? If there are no other classes using this- then its not a generic functionality.

Lalit Mehra wrote: brother i have explicitly written (inner class) please check ...


Though it can be implemented. I feel its Not real world situation example. And do you think the example you have provided is useful?
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes is do think so ... and by the term generic i meant something that shouldn't be coded in the Dog class ... and i have also written there is no other class to use that functionality ...

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:there can be a lot of examples ... for suppose

you have two classes

1. Animal
2. Dog

Now let's say Dog is your core class that you have to work with ... but just for some requirements you need some generic functionality and you do not want to put that in the Dog class ...

here you go ... you can make another class Animal (inner class) and write your generic code in it ...


and access it from within the Dog class itself ...



Please show the complete code and tell us which real world app could use this code ? Or if you have written some code other than the dog-cat example, please show that as well.
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i asked you to suppose it ...

anyways ... just for the sake of providing real world examples ... go through the java API documentation for Collections and Generics ... JAVA API

for the best of all ...

keep coding
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote:
anyways ... just for the sake of providing real world examples ... go through the java API documentation for Collections and Generics ... JAVA API


How would Java API provide example for Inner classes? If at all you would ask to refer to the source code- that might have been useful

Rahul, I could just recall that- Lot of places in JDK source code inner classes have been used. You might want to check the Stream related classes in Java IO package (Not sure about the exact classes which use them, someone might be able to point few example classes which use).
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:

How would Java API provide example for Inner classes? If at all you would ask to refer to the source code- that might have been useful



there are many classes in JAVA API which have inner classes .... anyways leave it ... either i'm not being able to understand you or the same is the case with you ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic