![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 7
Rep Power: 0
![]() |
Super Class and Sub-Class data access
Hey i was wondering how to access private data from Super class in the sub-class without changing the data to protected. Or is that not possible? My teacher wants me to keep encapsulation so all my data has to stay private.
|
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2006
Posts: 7
Rep Power: 0
![]() |
i think my teacher wants me to use a different way to access the private data indirectly or something
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 3
![]() |
You don't modify the instance variables of a super class, you get a copy of it in a subclass and modify that.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2006
Posts: 7
Rep Power: 0
![]() |
yeah i got it. He wanted setters and getters. Stupid question, it was too late at night for me to think. =P
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() |
that a pretty basic Object Oreiented question. I would check out This here link and read it, mostly the part about Access modifiers.
What you are looking to do is setup some private varibles and a method to set the private varible and to get the current value of that varible. Private varibles can only be accessed from inside the class, which is why you have to have get and set methods. let's say we have a class called Dog. In that class we would have to setup some varibles to hold the various attributes that a normal dog would have, like eye color, hair color, weight etc... The varibles that actually hold the data for each dog should be set as private, because the data is speciffic to that Dog. When you code in OO, you reduce your programming time, because you reuse code. So if you had lets say a program that stores a list of dogs, you would only have to code the Dog class once, and just reuse it. Every time you create a new dog you would use the Dog class constructor, which in turn would ether set the various attributes it's self or call the set methods of that class. Don't worry that article that i posted above explains quite a bit. I think it will get you pointed in the right direction.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
What if you had a subclass, Doberman? Would you not then want to make those variables protected? This is assuming, of course, that Doberman class needed access to those variables.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|