• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if super class is abstract, subclass must be abstract?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic