It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract" Watch "Abstract" New topic
Author

Abstract

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
what is Abstract? Is it keybword? If method is declared as abstract method? what does this mean? What is the advantage of declaring method as an abstract?
Plz explain me. I read one book, but still my concept is not clear!!!
Thanks in advance,
Angela
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Yes, abstract is a keyword.
When you are making classes, sometimes you can conceive of a class that would contain all sorts of common behaviour, but doesn't make any sense all by itself.
The typical example used in some textbooks is "employee". The superclass Employee would have things like FirstName, LastName, IDNumber, etc.... things that are common to every employee.
But wages are something that is different, depending on whether you are a term employee, contract employee, pieceworker, hourly, etc..
So employee all by itself, does not make enough sense... What good would an employee object be, if it didn't contain wage information. Only subclasses of employee such as "HourlyWorker" and "ContractWorker" make sense. To ensure that programmers only create classes that are subclasses of Employee, you can make the Employee class abstract.
You can apply the same thinking to methods.
There is some confusion about when abstract classes are appropriate to use or not. The textbook I use doesn't seem to indicate any 'best practices'
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Yes abstract is a reserved word in Java. It can be used on both methods and classes.
When you declare a class/method as abstract you are saying that a subclass/method will implement the code in the abstract method/class. You cannot call an abstract classes constructer, hence you cannot instantiate an abstract class. Heres a crude example:
class car
{
abstract void gasolineType(){}
abstract void motorSize(){}
}
Here you have a class named car. The car class has two methods in it but no code inside the method. So therefore any subclass of the car class must implement those methods that are found in the car class. So you could have a subclass like this:
class honda extends car
{
void gasolineType()
{
// code goes here to actually fill out the method
}
void motorSize()
{
// more code here
}
}
I hope that helped a bit. Im sure someone else can explain it much better though.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Andy,
Good example, the only thing wrong is that in your abstract method declarations in class car, you need to omit the braces. Abstract methods just end in a semicolon, like so:
abstract void motorSize();
Other than that, your post is a perfect explaination. One thing I might add: If a class contains any abstract methods, the class itself is implicitly abstract as well. But, the reverse is not true - if a class is abstract, all methods it contains do not necessarily have to be abstract.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Thanks for correcting me on that.
Howard Ralston
Ranch Hand

Joined: Jun 25, 2001
Posts: 105
Ryan,
I thought that if a class declares an abstract method for which
it does not provide implementation, then that class MUST be declared abstract. Please correct me if this is wrong.
Thanks,
Howard


<a href="http://www.getlocaldeals.com" target="_blank" rel="nofollow">Free local coupons</a>
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Yeah i think Ryan said that.
One thing I might add: If a class contains any abstract methods, the class itself is implicitly abstract as well.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi,
Andy: That's not what Ryan said, and...
Ryan: I think what you said is wrong.
If you have a class that contains an abstract method, you must *explicitly* declare this class as abstract. (So Howard, you are correct).
But also - Howard said "I thought.... an abstract method for which it does not provide implementation..."
If you attempt to provide an implementation on an abstract method, you will get a compiler error. Even a blank body ( {} ) like you could do in C++ would be wrong.
The second thing Randy said is correct... An abstract class need not have *all* it's methods abstract (the methods in an abstract class are *not* implicity abstract)
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Mike can you please clarify some things for me?
If you have a class that contains an abstract method, you must *explicitly* declare this class as abstract.

What happens if you dont declare it as abstract? The compiler will yell at you?
If you attempt to provide an implementation on an abstract method, you will get a compiler error. Even a blank body ( {} ) like you could do in C++ would be wrong.

I dont get that. I thought the whole purpose for abstract methods was that you have to provide implementation in a sub class?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hey there Andy,
First:
Yes, the compiler will yell at you if you do not *explicitly* declare a class abstract, if that class contains an abstract method. It will say:

Second:
Oops, I should have been more clear. If you attempt to provide an implementation for an abstract method in the class in which it is being declared abstract, then you get a compiler error, to whit:

So just as you think, in subclasses of this Test class, you can (and *must*) provide implementation for the abstract class.

If, in fact, you do *not* provide an implementation for each abstract method in a subclass of an abstract superclass, then those unimplemented methods remain abstract, and guess what? You must *explicity* declare your subclass to be abstract as well.

Chew on that!
[This message has been edited by Mike Curwen (edited March 29, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes, if you have even one abstract method the compiler will complain if you do not explicitely declare the class abstract. (Note that you CAN declare a class abstract even if it has NO abstract methods).

From the JLS 8.0

A named class may be declared abstract (�8.1.1.1) and must be declared abstract if it is incompletely implemented;

As for method syntax:
The point is that if you try to provide an implementation for an abstract method in the method that is declared abstract the compiler will complain.
When you override it in a subclass and provide and implementation you remove the keyword abstract.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks to all ,
So finally I am cleared about Abstract.And also If you declared Abstract Method, class MUST BE DECLARED AS Abstract.
But I want to be more clear. I can understand the advantage of declaring Abstract method. BUT IF YOU DON't USE Abstract method, then what is advantage having declare class as an Abstract.
Please let me know,
Angela
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Thanks for the clarification Mike.
Your explanation cleared things up alot. At first you threw me for a loop with your post, but then when you explained it it made things perfectly clear. Thanks.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
But I want to be more clear. I can understand the advantage of declaring Abstract method. BUT IF YOU DON't USE Abstract method, then what is advantage having declare class as an Abstract.
A class declared as Abstract can not be instantiaited.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Originally posted by Angela Jessi:
But I want to be more clear. I can understand the advantage of declaring Abstract method. BUT IF YOU DON't USE Abstract method, then what is advantage having declare class as an Abstract.
Please let me know,
Angela

Angela
Abstract classes are something you cannot instantiate
for example
lets take the class Shape
abstract class Shape
{
public void draw(){}
}
Its abstract. You know its a shape, you cannot create one.
if you have to create one you have to know what kind of shape - circular? rectangular? triangular?
So I create
class Circle extends Shape
{
}
likewise
class Triangle extends Shape
{
}
You can create a circular shape. There is no raw shape exists.
In this case Shape is called "base" or "parent" or "super" class
whatever extends this shape called "derived" or "child" or "sub" class.
Hope this helps
cheers
Siva
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks a bunch Siva!!:-)
But I want to know conceptually benefit of declaring abstract class without abstract method inside it.
Thanks again,
Angela
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Originally posted by Angela Jessi:
Thanks a bunch Siva!!
But I want to know conceptually benefit of declaring abstract class without abstract method inside it.
Thanks again,
Angela

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You would do it when you want to force developers to use the sub-classes of your abstract class. Imagine an abstract class with no abstract methods. It has two child classes that override different methods.
public abstract class AbstractA {
public void method1() {
//do stuff
}
public void method2() {
//do stuff
}
}
public class Concrete1 extends AbstractA{
public void method1() {
// do different stuff
}
}
public class Concrete2 extends AbstractA{
public void method2() {
// do different stuff
}
}
I have this exact situation in one of my applications. We never want the abstract class instantiated even though it has no abstract methods because the object would have no functionality without overriding at least one method.
[This message has been edited by Thomas Paul (edited March 29, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I think this is where a lot of Java textbooks are somewhat unclear on benefits/advantages.
for example, Deitel & Deitel make this Software Engineering Observation:
" An abstract class defines a common interface for the various members of a class hierarchy. The abstract class contains methods that will be defined in the subclasses. All classes in the heirarchy can use this same interface through polymorphism."

So then... the used the word interface a few times, and indeed, abstract superclasses (in most cases) can be replaced by interfaces. Unless of course, the abstract classes contain some methods that are not abstract (ie: they have implementation), in which case, an interface could not substitute.
Muddying the waters...
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The key here is "common interface". Even if the abstract class has no abstract methods, it provides a common interface that can be shared among multiple child classes.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Little

Originally posted by Thomas Paul:
The key here is "common interface". Even if the abstract class has no abstract methods, it provides a common interface that can be shared among multiple child classes.

ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Oopsie, not sure what I was thinking of when I said that "implicitly abstract" stuff.
Thanks for setting me straight guys.
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstract
 
Similar Threads
ABSTRACT
abstract classes
abstract
abstract method
Interface