This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract nature with package level access? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract nature with package level access?" Watch "abstract nature with package level access?" New topic
Author

abstract nature with package level access?

Kkush Garg
Greenhorn

Joined: Oct 14, 2011
Posts: 12
// 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...



Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Every time you extend an abstract class, you must override all abstract methods.

So, your class JR3 must override the method abstract void showIt();.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Kkush Garg wrote:member with package access are not inherited


I'm pretty sure this is an incorrect statement.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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...


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
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

Joined: Oct 14, 2011
Posts: 12
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

Joined: Jul 19, 2011
Posts: 185
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
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 14, 2011
Posts: 12
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

Joined: Jul 19, 2011
Posts: 185
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

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Apr 06, 2010
Posts: 4397
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
GeeCON Prague 2014
 
subject: abstract nature with package level access?