File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes need of overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need of overriding" Watch "need of overriding" New topic
Author

need of overriding

shreehari Gopalakrishnan
Ranch Hand

Joined: Jun 01, 2004
Posts: 30
Dear members

when ever i studied about the overriding technique i had a doubt about its need. it may be a simple question to java masters but to whom ever i asked i have'nt got an apt answer.if we declare a function signature in a class and implements that in other class then what is the need of that signature
can,nt we directly extend the class which have implementation and use it?
it is a very basic question so can i expect a very nutshell answer from the members?

thank U
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You're right about one part. Say we write class A with a nice method doSomething(). Then we write class B that extends A. You can make an instance of B and call doSomething() and the code written in class A runs. That's perfect in many situations.

But we have the opportunity to make B doSomething() slightly different than A did. Maybe A gets some data from a file and you'd like B to get the data from a database. Then you can "override" the method in B to do anything you'd like. This is pretty much the definition of polymorphism - making the same method take two forms.

There's a bit of danger in this. Say there's a class C that accepts an instance of A as a parameter to a method. And C then calls doSomething(). Now C may depend on how A did things in some unforseen way. Maybe it puts data into the file that A is going to read. It is perfectly legal to pass C an instance of B in that parameter. C might write the file and call doSomething() and NOT get the results it expects because B reads a database.

A golden rule to help avoid this problem is never extend a concrete class, only extend abstract classes or implement interfaces. Google for "Liskov Substitution Principle" or see this article Why Extends Is Evil for some fairly advanced discussion on the topic.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranc shreehari!

Please notice this posts has nothing to do with threads and we have a nice beginner's forum that would have been more appropiate for it.


SCJP2. Please Indent your code using UBB Code
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Jose Botella:
this posts has nothing to do with threads


Indeed. Moved to Java in General (Beginner).


[Jess in Action][AskingGoodQuestions]
shreehari Gopalakrishnan
Ranch Hand

Joined: Jun 01, 2004
Posts: 30
First of all a big SORRY to all the administrators.
Because that was by first message to this wonderful site book

Thank you very much Stan James for your reply

I understood but still with a doubt (may be a stupid one)
if we are implementing differently in different derived classes then what is the need of that signature.
can't we avoid that extension

for example

class A{
public void a();
}
class B Extends A
{
public void a()
{
SOP("hello B");
}
}
class C extends A
{
public void a()
{
SOP("hello C");
}
}

My question is what is the need of this extends A
any way we are not doing anything in the function a() in class A



can U please answer this
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just as an example, you get into interesting situations like this in frameworks. My definition of framework usually involves something about "inversion of control" so instead of controlling the sequence of every thing that happens in your program you turn some control over to the framework. (If you wrote the framework, too, you might be turning control over to yourself, but the ideas still work.)

Let's say a framework tells us when new mail messages arrive by calling method a(). The framework author provides a base class with an empty a() method. When you extend the class, you override a() to do something useful with the message. Maybe you have one extended class that shows messages on screen and another extended class that puts messages into a file. And if another extende class doesn't override a() then nothing happens at all, which might be exactly what you want some times.

One bit left out ... how do you plug your extended classes into the framework? You'd probably "register" them with something like:

Or maybe the framework only uses one at a time:


So, an abstract framework class might have lots of empty methods. You can override them or not to make interesting things happen. Zat help?
[ June 02, 2004: Message edited by: Stan James ]
shreehari Gopalakrishnan
Ranch Hand

Joined: Jun 01, 2004
Posts: 30
Thanks stan

What i understood is if we r writing simple softwares then no need of overriding we can directly extend the class. In case for big softwares which uses a frame work like struts or cleveland architecture we have to conventrate on overriding. sorry if i wrote anything wrong


---------------------------------------------------------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need of overriding