• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need of overriding

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Indeed. Moved to Java in General (Beginner).
 
shreehari Gopalakrishnan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


---------------------------------------------------------------------------
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic