• 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

is it valid to declare an inherited method as abstract?

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...........

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic