• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

abstract nature with package level access?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// jR2.java

package one;

public abstract class jR2
{
abstract void showIt();
}


// jR3.java

package two;

class jR3 extends one.jR2{} // point A


here is an error at point A, says "two.jR3 is not abstract and doesn't override abstract method showIt() in one.jR2" why..???
as member with package access are not inherited then how class jR3 can be forced to override it....???
as overriding depends on inheritance...



 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time you extend an abstract class, you must override all abstract methods.

So, your class JR3 must override the method abstract void showIt();.
 
Sheriff
Posts: 28372
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kkush Garg wrote:member with package access are not inherited



I'm pretty sure this is an incorrect statement.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A concrete class such as jR3 must implements showIt() method of the abstract class jR2. The rule is any concrete class that extends an abstract class must implement the method it extends, but for the abstract class an implementation of the method is optional.




See K & B Chapter 1 Declarations and Access Control, page 44 on Figure 1-5 for more info and reinforce your knowledge.

Hope it help...
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm, not sure.
What if a method in JR2 depends on showIt():

If showIt() is not overridden, then this cannot work.
On the other hand declaring a showIt() in JR3 is not overriding, but declaring a new method (as the showIt() from JR2 is not inherited) ...
I tried it out and when I declare a showIt() in JR3 the error message still appears.

John
 
Kkush Garg
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know that an concrete class must implement all abstract methods from the superclass but implementing an abstract method is like overriding and why we should override
the method even it is not inherited.......

and when we try to override that in jr3 compiler still shows same error then why...???
 
John Stark
Ranch Hand
Posts: 185
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it looks like you just cannot do class jR3 extends one.jR2 as there is a non-overridden abstract method in JR2 which you are not able to override. Whether inherited or not you cannot have non-overridden abstract method in a superclass of a non-abstract class.

John
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kkush Garg wrote: i know that an concrete class must implement all abstract methods from the superclass but implementing an abstract method is like overriding and why we should override
the method even it is not inherited.......



It is inherited. It's just not visible to the subclass. But it could still be called by a class where it is visible - so there has to be an implementation.
 
Paul Clapham
Sheriff
Posts: 28372
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to spell it out -- if I have it right:

(1) That abstract class's definition says that any subclass must provide an implementation of the showIt() method, which has default access.

(2) Only classes in the same package as the superclass can override superclass methods which have default access.

Therefore two.jR3 cannot extend one.jR2.

(I haven't considered what happens if two.jR3 tries to override the showIt() method with an abstract method. Left as an exercise for the interested student.)
 
Kkush Garg
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with this that all members are actually inherited in the subclass including private members mathew sir ,, but i still not able to digest the code because
then two.jR3 should be allowed to override showIt() of one.jR2......
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

then two.jR3 should be allowed to override showIt() of one.jR2......


So maybe it is bad design having the class public and the abstract method with default access?

John
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kkush Garg wrote:i agree with this that all members are actually inherited in the subclass including private members mathew sir ,, but i still not able to digest the code because
then two.jR3 should be allowed to override showIt() of one.jR2......


But if it was allowed to override it, that would break the meaning of the access specifiers, which would be bad.

As John suggests, I don't think it's a very good design, which is why you're running into this issue.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:(I haven't considered what happens if two.jR3 tries to override the showIt() method with an abstract method. Left as an exercise for the interested student.)


I tried this last time I looked into a similar problem. Yes, it works. You can then (if you make jR3 public) write a non-abstract one.jR4 extending two.jR3, and implement showIt() there (because it's visible again), so allowing it isn't completely pointless. Though why you might want to do that is another matter.
 
Paul Clapham
Sheriff
Posts: 28372
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Though why you might want to do that is another matter.



That's why the question arose in the Programmer Certification forum, isn't it? Pretty much every piece of code you see posted here is something you really wouldn't want to be doing in real life.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic