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 is it valid to declare an inherited method as abstract? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "is it valid to declare an inherited method as abstract?" Watch "is it valid to declare an inherited method as abstract?" New topic
Author

is it valid to declare an inherited method as abstract?

srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
Hi,



true
false

ans:true


i was shocked to know the answer.if a method is abstract then
even the class should also be declared as abstract thats the exact
error which i have got so whats happening here guys???

is my understanding of the above statement wrong??


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi !

i think it is valid to declare
an inherited method as abstract,bcoz
what you actualy do is just pass on its (methods)
implementation to its base class...........

Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
It does not compile, because "a" has to be declared abstract.

But after changing "a" to abstract, it compiles and non-abstract method() in class base is overriden by abstract method() in class a.
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
I am getting this error....in 1.4
why?


---------- compiler ----------
ab4.java:8: a4 is not abstract and does not override abstract method method() in a4
class a4 extends base4
^
1 error

Output completed (59 sec consumed) - Normal Termination
Ashok Guntuku
Greenhorn

Joined: Feb 12, 2007
Posts: 3
package myabstract.inherits.concrete;
class GrandPa{
public void mymethod() {
System.out.println("ur inside grand pa method");
}
}

-------------

package myabstract.inherits.concrete;
abstract class Pa extends GrandPa{
public abstract void mymethod();
}

-------------

package myabstract.inherits.concrete;

public class Son extends Pa{
public void mymethod(){
System.out.println("Its now in Son's method");
}
}

--------------

package myabstract.inherits.concrete;
public class Test {
public static void main(String args[]) {
Pa b1=new Son();
b1.mymethod();
System.out.println(b1.getClass());
GrandPa b2 = new GrandPa();
b2.mymethod();
System.out.println(b2.getClass());
}
}

--------------

I think the above code is self-explanatory.
We can have an abstract class override a concrete method in its super class.


by the way, can anybody tell me how to invoke the GrandPa's method using Son's instance.
Ashok Guntuku
Greenhorn

Joined: Feb 12, 2007
Posts: 3
here is the output for last post
------------
Its now in Sons method
class "myabstract.inherits.concrete.Son"
You are inside grand pa method
class "myabstract.inherits.concrete.GrandPa"
Ashok Guntuku
Greenhorn

Joined: Feb 12, 2007
Posts: 3
here is the output for last post
------------
Its now in Sons method
class "myabstract.inherits.concrete.Son"
You are inside grand pa method
class "myabstract.inherits.concrete.GrandPa"
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
by the way, can anybody tell me how to invoke the GrandPa's method using Son's instance.


By using GrandPa's instance.


Since in this example we have one of the class in hiearchy which does not have concrete implementation of method named "method()" we cant use here syntax super.<name of overidden method in superclass>.

If the class "Pa" would have had concrete implementation of method named "method()" then you can use super.method() in both Pa and Son class.

Which would have allowed you to access GrandPa's version of "method()" from Son.


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is it valid to declare an inherited method as abstract?