• 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

Override question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have an abstract class Base which implements interface If1.
Classes D1, D2 and D3 are derived from Base.
Right now, the "common" methods from If1 are implemented only in the Base class and the specialized methods are implemented directly in the derived classes. The "common" methods are not implemented in any way in the derived classes (they are just inherited). For example:
interface If1 {
void common_m1();
void m2();
}
public abstract class Base Implements If1 {
public void common_m1() {
// implementation
}

}
public class D1 extends Base {
public void m2() {
// D1 specific implementation
}

}
public class D2 extends Base {
public void m2() {
// D2 specific implementation
}

}
public class D3 extends Base {
public void m2() {
// D3 specific implementation
}

}
There is a proposition to implement all the methods in the Base class and to override all the methods in the derived classes. For common methods, the method from the derived class will use super to call the base class implementation, like this:
interface If1 {
void common_m1();
void m2();
}
public abstract class Base Implements If1 {
public void common_m1() {
// implementation
}

public void m2() {
// dummy implementation
}
}
public class D1 extends Base {
public void common_m1() {
super.common_m1();
}
public void m2() {
// D1 specific implementation
}

}
public class D2 extends Base {
public void common_m1() {
super.common_m1();
}
public void m2() {
// D2 specific implementation
}

}
public class D3 extends Base {
public void common_m1() {
super.common_m1();
}
public void m2() {
// D3 specific implementation
}

}
Is there any reason why the second solution is better than the first?
Thank you,
Bobby
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me the second is good if you just started programming and to somewhat avoid so much confusion. What i cud suggest is that you don't need an interface if Base is already abstract: just declare the methods to be abstract! You'll save yourself cuonfusions... hehehehe...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only benefit that comes to mind is that the non-abstract base classes wouldn't have to implement the method if they didn't need it, making their code a little cleaner.
Note that this is what the poorly named "Adapter" classes in the java.awt.event package do - they provide empty implementations of methods declared in some listener interface.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest the first one.
Empty method implementations in base class has no meaning to me,Unless some of the derived classes doesn't need to implement those abstract methods..
So it depends totally on ur requirements and future extensions..
Regards,
Jelda
 
reply
    Bookmark Topic Watch Topic
  • New Topic