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 Abstract Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Class" Watch "Abstract Class" New topic
Author

Abstract Class

Larry Lai
Ranch Hand

Joined: May 13, 2002
Posts: 44
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Class