Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it valid to declare an inherited method as abstract?

 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic