Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 29th, 2007, 6:03 AM   #1
ThatOnePhu
Newbie
 
Join Date: Nov 2006
Posts: 7
Rep Power: 0 ThatOnePhu is on a distinguished road
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.
ThatOnePhu is offline   Reply With Quote
Old Jan 29th, 2007, 7:09 AM   #2
ThatOnePhu
Newbie
 
Join Date: Nov 2006
Posts: 7
Rep Power: 0 ThatOnePhu is on a distinguished road
i think my teacher wants me to use a different way to access the private data indirectly or something
ThatOnePhu is offline   Reply With Quote
Old Jan 29th, 2007, 8:59 AM   #3
Serinth
Programmer
 
Serinth's Avatar
 
Join Date: Sep 2005
Posts: 50
Rep Power: 4 Serinth is on a distinguished road
You don't modify the instance variables of a super class, you get a copy of it in a subclass and modify that.
__________________
A girl talked to me once.

http://www.latestanime.com
Serinth is offline   Reply With Quote
Old Jan 29th, 2007, 9:17 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by ThatOnePhu View Post
i think my teacher wants me to use a different way to access the private data indirectly or something
My guess is that your teacher wants you to create getter and setter methods to access and alter the private variables.
Arevos is offline   Reply With Quote
Old Jan 29th, 2007, 1:00 PM   #5
ThatOnePhu
Newbie
 
Join Date: Nov 2006
Posts: 7
Rep Power: 0 ThatOnePhu is on a distinguished road
yeah i got it. He wanted setters and getters. Stupid question, it was too late at night for me to think. =P
ThatOnePhu is offline   Reply With Quote
Old Jan 29th, 2007, 1:16 PM   #6
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Old Feb 15th, 2007, 9:59 AM   #7
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
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.
Harakim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC