dcsimg
abstract classes weird
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Giles_Bowkett
Posted On:   Wednesday, September 4, 2002 01:42 PM

abstract class Abstraction {  public abstract void doStuff() ; } public class AbstractTest extends Abstraction {  public AbstractTest () {  }  public void doStuff() {  }  public static void main (String args[]) {   AbstractTest at = new AbstractTest() ;   at.doStuff() ;  } } so this works, but the thing is it seems strange that it does. basically the only difference between the two doStuff() methods is that one is declared abstract and has a semicolon, and the other is not and has an empty body   More>>

abstract class Abstraction {

 public abstract void doStuff() ;

}



public class AbstractTest extends Abstraction {



 public AbstractTest () {

 }



 public void doStuff() {

 }



 public static void main (String args[]) {

  AbstractTest at = new AbstractTest() ;

  at.doStuff() ;

 }

}



so this works, but the thing is it seems strange that it does. basically the only difference between the two doStuff() methods is that one is declared abstract and has a semicolon, and the other is not and has an empty body.



this is easy to remember for the test, you just remember the punctuation differences -- but doesn't it seem kinda weird? although you have to implement all the methods of an abstract class in order to make a subclass you can instantiate, you don't have to provide any actual implementation of those methods when you implement them. you can just "implement" them with an empty method body. it somehow seems arcane and pointless to me. what's the real difference between that and an abstract method?

   <<Less

Re: abstract classes weird

Posted By:   Steven_Rose  
Posted On:   Wednesday, September 11, 2002 02:26 PM

The main difference between an abstract class and a concrete class is that an abstract class represents an abstract concept for which an instance cannot exist.



For example, if I ask you to imagine a shape without picturing a *specific* shape (like a circle, square, or many-sided polygon), you can't do it. Similarly, it wouldn't make sense to define a Shape class in Java that could be instantiated. What does it mean to create an instace of a "shape"? Instead, Shape should be an abstract class that is extended by concrete classes like Circle and Trapezoid.



When would you want to implement an abstract method by simply declaring an empty method body? Well, this is used quite often in the event model in Java. Say, for instance, that you are implementing the MouseListener interface so you can respond to mouse events (remember, an interface is different from an abstract class, but not in the sense that it provides abstract methods which you may want to override with an empty method body). You may want to respond only to certain events and not others defined in the MouseListener interface. So, you would implement a method body for the events that you want to receive and do something with. For the events that you want to just ignore (for instance, say you don't care about mouse rollover events), you would explicitly implement an empty method body on that method because you don't want to do anything in response to it.



Incidentally, whenever it makes sense to implement an abstract method with an empty method, you should put a comment in the method body: // do nothing. This lets other developers know that it was not simply an oversight. Or, you can place the braces up on the same line as the method declaration, this makes it pretty clear that you didn't mean to do anything:



public void doStuff() {}



sev

Re: abstract classes weird

Posted By:   Lasse_Koskela  
Posted On:   Thursday, September 5, 2002 01:27 AM

The idea of an abstract method is that every subclass must implement it. That's all. It's insignificant whether the method implementation contains 0 statements or 1000 lines of code. The point is that the client code using the implementation class through the abstract class can trust that the method is implemented technically, although not logically.


Why are you bothering yourself with this? If you don't need an abstract class, don't use one.

About | Sitemap | Contact