• 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

Abstract class and Access modifier

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm getting ready to pass the Programmer exam and I can not find the answer to the following scenario:

package mypackage.oneleveldown;
public abstract class MyAbstractClass {
public abstract void getPublicValue();
protected abstract void getProtectedValue();
abstract void getDefaultValue();
}

package mypackage;
import mypackage.oneleveldown.MyAbstractClass;
public class MyAbstractClassImpl extends MyAbstractClass {

}

I know that class MyAbstractClassImpl must implement the getPublicValue() and getPProtectedValue() method but what about the getDefaultValue() method?

thanks,
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If a concrete (non-abstract) class extends an abstract class, you MUST implement all the abstract methods in the abstract class.

You have an additional problem in that one of the abstract methods in the abstract class is not visible to your class that needs to do the implementing.

Try compiling it and see...

Si.
 
Assad Bouayed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use JBuilder 2005 and it does not compile because it wants me to implement the getDefaultValue() method. When I implement the method, it still complaints that the method is not implemented?
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You could...

1) Change the abstract method access modifer to public or protected.
2) Or move your implementing class into the same package as the class it is implementing and you can leave the method with default (no keyword) access in the abstract class.
3) Or mark your implmenting class abstract.
4) Or implemtent the method in the abstract class.

Cheers,

Si.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getDefaultValue() method has default access modifier. So it cannot visible from the outside the package which defined. Here, it is not visible to myPackage. So compiler always complaint "it is not implemented".
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, i guess its a question of designing your java application that you mark certain abstracted behavior to be protected so that subtypes are not proliferated all over the packages. Please correct me if i'm wrong.
thanks
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding this question i got a statement in JLS which says:


It is a compile-time error to declare an abstract class type such that it is not possible to create a subclass that implements all of its abstract methods.
...
...
....
A class type should be declared abstract only if the intent is that subclasses can be created to complete the implementation


We are trying to create a sub class of an abstract class which is not able to implement all of its abstract methods. So we get a compilation error, right?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic