• 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

Working with Abstract Classes

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

In Photograph.java, I need to have a constructor that should access parent abstract class to change its instance variables. Item's instance variables are private and there is no set method for them either. I am not suppose to change Item class in anyway.

How can I change parent abstract class's instance values while they are private and do not have any set methods?

This may not be most ideal situation to handle things, I understand.

Photograph.java


Item.java


Sellable.java


Error

----jGRASP exec: javac -g Photograpgh.java
Photograpgh.java:7: error: constructor Item in class Item cannot be applied to given types;
  public Photograpgh(int idNew, String nameNew, double priceNew){
                                                                ^
 required: int,String
 found: no arguments
 reason: actual and formal argument lists differ in length
Photograpgh.java:8: error: id has private access in Item
     this.id = idNew;
         ^
Photograpgh.java:9: error: name has private access in Item
     this.name = nameNew;
         ^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:
How can I change parent abstract class's instance values while they are private and do not have any set methods?



Simply. You can't. The private access modifier means that it is private to the class. It can't be accessed outside of the class, even to its subclasses.


... but, to answer the intent of your question. Your abstract base class does provide a constructor (that is not private) that can be used to set the variables. Why can't you used that? Or do you really need to change it after initialization?

Henry
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Superclass's constructor is protected, which means it's not accessible to sub-classes. Am I wrong on that?

Also, how do I change values of super class instance variables through its constructor of a subclass?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:Superclass's constructor is protected, which means it's not accessible to sub-classes. Am I wrong on that?



The protected access level allows access to subclasses, so, your subclass may call the superclass' protect constructor.

Recaip Sanli wrote:
Also, how do I change values of super class instance variables through its constructor of a subclass?



Well, technically, you can't "change" the values... but you can call the superclass constructor from the subclass constructor (using the super() statement), and initialize the instance variables of the superclass.

Henry
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sense! Thank you @

Replacing this


With this



fixed the problem!
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you @Henry Wong
reply
    Bookmark Topic Watch Topic
  • New Topic