Abstract
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Plz explain me. I read one book, but still my concept is not clear!!!
Thanks in advance,
Angela
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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'
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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>
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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>
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
One thing I might add: If a class contains any abstract methods, the class itself is implicitly abstract as well.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
,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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
A class declared as Abstract can not be instantiaited.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.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
But I want to know conceptually benefit of declaring abstract class without abstract method inside it.
Thanks again,
Angela
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).]
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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...

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks for setting me straight guys.
------------------
- Ryan Burgdorfer
- Java Acolyte in
- Columbus, OH USA
<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
| No thanks. We have all the government we need. This tiny ad would like you to leave now: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











