aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Begginer About Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Begginer About "Inner Classes"" Watch "Begginer About "Inner Classes"" New topic
Author

Begginer About "Inner Classes"

salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
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 .
Mauro Mazzucco
Ranch Hand

Joined: Apr 26, 2013
Posts: 32

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
Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
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.
salih ayan
Ranch Hand

Joined: Apr 23, 2013
Posts: 59
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 ?
Rajesh Chokkalingam
Greenhorn

Joined: Jul 09, 2013
Posts: 15
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


Low Cohesitve Example:
=================
class BudgetReport {
void connectToRDBMS(){ }
void generateBudgetReport() { }
void saveToFile() { }
void print() { }
}

High Cohestive example
=================
class BudgetReport {
Options getReportingOptions() { }
void generateBudgetReport(Options o) { }
}
class ConnectToRDBMS {
DBconnection getRDBMS() { }
}
class PrintStuff {
PrintOptions getPrintOptions() { }
}
class FileSaver {
SaveOptions getFileSaveOptions() { }
}
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 773
Salih,
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.

Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

Hi Salih,

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.

HTH,
Ben


Twitter:ben_pheonix
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Begginer About "Inner Classes"