• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working with Abstract Classes  RSS feed

 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 72
2
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sense! Thank you @

Replacing this


With this



fixed the problem!
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @Henry Wong
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!