• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class

 
Larry Lai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Why abstract class can not be created an ojbect?
Is the reason that the abstract method doesn't have any body(code and variable)?
Second,I'm not sure the keyword implements and extends?
thanks
Larry
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why abstract class can not be created an ojbect?

Is the reason that the abstract method doesn't have any body(code and variable)?


An abstract method does not have any body and that would cause the class that contains it to be abstract as well. However, you can have an abstract class that does not contain an abstract method. The keyword abstract prevents the class from being instantiated.

Second,I'm not sure the keyword implements and extends?

implements is used for interfaces (Yolk implements Cookable)
extends is used for classes (Yolk extends Egg)

where Yolk is a subclass of the Egg class and Cookable is an interface.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry,
There are similarities between an abstract class and an interface. Both can be used to define the signatures of methods even though the body of the method is not defined. For example, you might want to define a shape as an abstract class that has a method called area. A square might extend the abstract shape class and implement the area method by returning the length of a side squared. A Rectangle might extend the shape class and implement the area method by returning the length times the width.
If you want to implement the methods of an abstract class, then you extend the abstract class.

You could also declare Shape as an interface containing method declarations that have no body just as you might have done with an abstract class called shape. When you declare concrete classes such as Square and Rectangle you would implement the interface.

There are advantages and disadvantages associated with either approach. For example, an abstract class can also declare concrete method in addition to abstract methods. An interface can not declare concrete methods.
A class can implement any number of interfaces, but a class can only extend one abstract class.
All of the methods declared by an interface are implicitly public and must be implemented as public methods. An abstract class can declare protected methods.
[ July 13, 2002: Message edited by: Dan Chisholm ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

implements is used for interfaces (Yolk implements Cookable)
extends is used for classes (Yolk extends Egg)

Inheritance is an is-a relationship. It is correct to say that a scrambled egg is-a egg. Therefore it would be correct to model the relationship between a scrambled egg and an egg as an inheritance relationship. Scrambled egg would extend egg and would probably expose methods for doing the actual scrambling.
It is not correct to say that a yolk is-a egg. Instead, you would say that an egg has-a yolk. Therefore, it would be correct to model the relationship between a yolk and an egg as a delegation relationship. Similarly, you could say that an egg has-a yolk, shell, and egg white. You could then refer to the relationship as an aggregation or composition depending on which definition of the terms you choose--UML or Gang of Four (GoF).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic