Hi to everybody..
l am reading "SCJP SCJD 1.6 Kathy Sierra & Bert Bates" book. In that book page 663,664 explaning logic of using inner class by some example scenario.
In that pages(663,664) mentioning about "event handling and ChatClient class" something.May anybody explain me what does book want to explain or
as separately can any one explain logic of using inner class .
Inner classes are like "helper" class, your java classes should all be cohesive and have only one purpose, a class that connects to a database should not be the same that send messages or display the gui, thats why we have inner classes, they are classes that only exists if theres a instance of the outer class, its very rare to use em
Inner calsses are "one time use" class that need not be available to other classes, and only is relevant to the class in which it resides.
when you want to implement some classes behaviour or properties and that properties never needed for any other classes and it only tied with your classes, you can go for inner classes.
Also by using inner class instance can able access member of outer classes.
see chat client want to inherit all the events from separate classes and also it wnat to inherit some properites from some other classes. Here java wont allow multiple inheritance, in the same time event never going to use for any other classes. because events are chat client specific. So we go for inner classes events are placed in inner classes still chat client can extend any other class.
Joined: Apr 23, 2013
Mauro Mazzucco wrote:Inner classes are like "helper" class, your java classes should all be cohesive and have only one purpose, a class that connects to a database should not be the same that send messages or display the gui, thats why we have inner classes, they are classes that only exists if theres a instance of the outer class, its very rare to use em
Thanks for your explanation, but may you explain what cohesive is ?
Joined: Jul 09, 2013
Cohesion fully focus on how well single class has defined?
High Cohesion is desirable, low cohesion is undesirable. usually programmer want to develop code with high cohesion
If a class created with well encapsulation and that support for future implementations and enhancability with out deleting existing code, we can say that class have high cohesion. following examples taken from scjp 6 exam preparation book
Rajesh's example is a good one.
High cohesion can be at method level, class level and package level.
For example, high cohesion means doing one thing in one method, instead of doing two things in one method.
Use Rajesh's example,
For package level, you can see io package only deals with I/O, swing package only deals with graphical user interface, sql package only deals with database connection and query .....
Each package won't deal with other functions.
The book basically aims to make you understand that, high cohesion is always desirable as a goal of good object oriented design. For example a class focusing
on a booking, if it starts involving, reporting or some other functionality wont promote a good design. When you come to design patterns, even an inner class
can be a good candidate to be a part of Template pattern as well. On other hand, it is totally dependent. Just focus on high cohesiveness in your application that
your class should only be performing business it relates to. Not un-related business.