File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes if super class is abstract, subclass must be abstract? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "if super class is abstract, subclass must be abstract?" Watch "if super class is abstract, subclass must be abstract?" New topic
Author

if super class is abstract, subclass must be abstract?

hanmeng
Greenhorn

Joined: Aug 16, 2000
Posts: 26
Hi,
My question is:
if super class is abstract, the subclass must be abstract or all methods must be abstract? please read follow:
I do execise questions of Marcus Green Mock No.1 . I found the question is following:
it explain that: Any class derived from an abstract class must either define all of the abstract methods or be declared abstract itself.
==============================
abstract class MineBase {
abstract void amethod();
static int i;
}
public class Mine extends MineBase {
public static void main(String argv[]){
int[] ar=new int[5];
for(i=0;i < ar.length;i++)
System.out.println(ar[i]);
}
}
3) Error Mine must be declared abstract Any class derived from an abstract class must either define all of the abstract methods or be declared abstract itself.
==================
But I found another question in Marcus Green Mock No.2. it same Structure, but different result:
abstract class Base{
abstract public void myfunc();
public void another(){
System.out.println("Another method");
}
}
public class Abs extends Base{
public static void main(String argv[]){
Abs a = new Abs();
a.amethod();
}
public void myfunc(){
System.out.println("My func");
}
public void amethod(){
myfunc();

}
}
1) The code will compile and run, printing out the words "My Func"
Alexandr Pravdenko
Greenhorn

Joined: Aug 04, 2006
Posts: 1
You "inherit" from abstract classes.
Abstract classes are incomplete that
requires specialization.
You use abstract classes to initiate a hierarchy of
special classes.

Abstract classes cannot be instantiated.
A class that has abstract methods must
be declared abstract! An abstract class can
have a final method ( the final method cannot be
overridden but can be inherited ) .
Subclasses of abstract methods must provide
implementations of inherited of abstract methods
before being called!
All methods of abstract classes are public
implicitly and must be public when implemented.

Subclasses that do not provide an implementation
of its inherited methods is also abstract!
Shankar G
Greenhorn

Joined: Sep 24, 2000
Posts: 16
In other words, u should provide implementation for all the super-class' abstract methods in the sub-class, unless u want the sub-class also be of abstract type.
-- Shankar.
kishen uchil
Greenhorn

Joined: Aug 21, 2000
Posts: 14
hi there hanmeng.
please refer to both the codes properly .in the first code the abstract method (amethod())from the abstract class(minebase)has not been implemented in the subclass(mine).
whereas in the second code the abstract method (myfunc())has been implemented in the subclass (abs)and thats why the compiler doesnot flag an error.
hope this helps.
bye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if super class is abstract, subclass must be abstract?