• 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

a question about two similar case ,one work while the other doesn't,why?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

read the code following(scjp_2_81).
//****************code1(scjp2_81)***********************//
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 arg[])
{
Abs a=new Abs();
a.method();
}
public void myfunc()
{System.out.println("My func");}
public void method()
{myfunc();}
}

//****************code2(scjp2_88)********************//
abstract class MineBase
{
abstract void amethod();
static int i;
}
public class Mine extends MineBase
{
public static void main(String arg[])
{
int[] ar=new int[5];
for(i=0;i<ar.length;i++)>
System.out.println(ar[i]);
}
}
//****************************************************//
the code_1 will compile and print out "my func";(but the myfunc is abstract ,will the Abs declare as abstract ?);
why the code1 compile while the code2 not ,the code2 complain that "the Mine must be declare abstract";
I had thought that a class that contain an abstract method must itself be declared as abstract. Any class derived from an abstract class must either define all of the abstract methods or be declared abstract itself.But now I not so sure .
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are extending an abstract class either you have to implement the abstract methods in super class if any or you have to declare the subclass also as abstract.
in first case you are implementing abstract method in the super class in subclass,so you don't have to declare the subclass as abstract.
but in second case you are not implementing the abstract method in super class, so you have to declare sub class also as abstract.
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I m not very clearly with what you said ?
would you kind enough to give me a detail,
thanks a lot.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii..
In the first example there is one abstract method - myfunc()
Since the sublass (in this case Abs) provides an implementation for this abstract method,there is no need to declare the subclass abstract.
In the second example also there is one abstract method - amethod()
But in this case since the subclass doesn't provide any implementation for this abstract method,subclass must be declared abstract.Otherwise it will give Error saying subclass(Mine) must be declared abstract
Bottomline is...
if the superclass is abstract...
1.the sub class should be defined abstract if abstract methods(if any)are not implemented in sub class.
2.the sub class should not be defined abstract if abstract methods(if any)are implemented in sub class.
hope this helps
chin

[This message has been edited by chin josei (edited July 10, 2001).]
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got it.
thanks a lot !
 
reply
    Bookmark Topic Watch Topic
  • New Topic