java - Understanding inner classes (Why inner classes exist) -


i reading thinking in java, why inner classes exist , problem solve.

the compelling reason book tries give is:

each inner class can independently inherit implementation. thus, inner class not limited whether outer class inheriting implementation.

please review understanding:

inner classes exist since java doesn't support multiple inheritance. (multiple inheritances) can done within inner class outer class can have multiple inner classes, , each of them can inherit different classes. in way, multiple inheritances can implemented. reason can think of inner classes address oop design principle composition better inheritance.

updated

most of explanation found answers below. example, inner class used in gui framework deal event handler. not mentioned reason quoted in book.i not saying answers below not good. actually. appreciated them(+1). want know there problem book?

it little puzzling why thought of idea of multiple inheritance after reading most compelling reason have quoted book. multiple inheritance comes question when class (inner or not) wants inherit behavior more 1 concrete implementation. thus, unlike other languages, in java, can not define class like:

class child extends father, mother {    // child wants inherit behavior father , mother } 

as can see, nothing only inner classes can rectify or work around java decision (not support multiple inheritance) in straightforward way.

then why exist, may wonder! well, in java every class either top-level or inner (also called nested). class defined inside class inner class , class isn't top-level class.

naturally, 1 might wonder why define classes (i.e. behavior) inside other classes. aren't top-level classes enough?

the answer yes. java have only top-level classes. idea (perhaps) there no reason restrict classes being members of other classes! predefined type (e.g. integer, string etc.) can member of class:

class person {   private string name; // field models person's name } 

a programmer should able define behavior of one's interest inside class:

class person {   private string name; // field models person's name   private address address; // address type defined here   static class address {     string street;      string city;   } } 

there's lot going on here, these things private, static etc. called modifiers. there many technical details them, let come them later. essential idea able define behavior part of class. address class defined outside person class, top-level class? of course. having facility comes in handy.

now, since facility introduced, started serving purpose , purpose called providing code data. how design patterns emerge , thought until 10 years ago inner classes can used provide data in form of code. perhaps puzzling you. consider following code that have taken verbatim jdk class: java.lang.string.java:

public static final comparator<string> case_insensitive_order                                      = new caseinsensitivecomparator(); private static class caseinsensitivecomparator         implements comparator<string> {      public int compare(string s1, string s2) {         int n1 = s1.length();         int n2 = s2.length();         // details excluded brevity         // return -1, 0, 1 appropriately     } } 

what has happened here?

we need way compare string string , need able case-insensitive comparison. so, created implementation of comparator interface right inside outer class: string! isn't handy? if inner class wouldn't there, have be:

public class string {   // ... whole string class implementation  } class caseinsensitivecomparator             implements comparator<string> {    // implements comparator method } 

and that's not 'bad' per se, means lot of classes polluting name space. inner classes restrict scope of behavior outer class. comes in handy, you'd perhaps see. data in case implementation of comparator interface , code well, same, because _new_ing inner class defined.

this feature exploited further using anonymous inner classes (especially in cases wanted code serve data) until java 7 , replaced lambda expressions in java 8. nowadays, might not see new code uses anonymous inner classes (in other words, language evolves).


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -