This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes can i have simple example or scenario where this is effective Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "can i have simple example or scenario where this is effective" Watch "can i have simple example or scenario where this is effective" New topic
Author

can i have simple example or scenario where this is effective

kumar reddi
Greenhorn

Joined: Apr 23, 2007
Posts: 8
Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Consider the following code:


The author of Class1 expects type to be set based on the String parameter value. There could be other methods in Class1 that depend on type being set. The author of Class1 assumes it was initialized because he/she set it in the constructor. If it is not set, other methods could throw errors or act unpredictably.

Now the Subclass author decides they would like to extend Class1. They create their own setType() method that does something completely different. Now Class1 doesn't do what it is supposed to anymore causing Subclass to break. Worse the Subclass author wasn't planning to override setType() and isn't aware of it.

The two solutions are to make the method private or final. Private allows Subclass to have its own setType() method. Final gives a compiler error so the Subclass author can name the method something else.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jeanne]: The two solutions are to make the method private or final.

Making the method static also works.


"I'm not back." - Bill Harding, Twister
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Originally posted by Jim Yingst:
[Jeanne]: The two solutions are to make the method private or final.

Making the method static also works.

In general yes. In my example, setType() access an instance field. So it can't be static.
kumar reddi
Greenhorn

Joined: Apr 23, 2007
Posts: 8
thank you very much for your clear explanation.
thanks.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Now Class1 doesn't do what it is supposed to anymore causing Subclass to break.

hey.. i m new to java.. i didnt understand this.. What doesnt class1 do that its supposed to?


Cheers!
RSR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can i have simple example or scenario where this is effective