Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Super Class and Sub-Class data access (http://www.programmingforums.org/showthread.php?t=12455)

ThatOnePhu Jan 29th, 2007 6:03 AM

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 Jan 29th, 2007 7:09 AM

i think my teacher wants me to use a different way to access the private data indirectly or something

Serinth Jan 29th, 2007 8:59 AM

You don't modify the instance variables of a super class, you get a copy of it in a subclass and modify that.

Arevos Jan 29th, 2007 9:17 AM

Quote:

Originally Posted by ThatOnePhu (Post 123219)
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.

ThatOnePhu Jan 29th, 2007 1:00 PM

yeah i got it. He wanted setters and getters. Stupid question, it was too late at night for me to think. =P

Pizentios Jan 29th, 2007 1:16 PM

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.

Harakim Feb 15th, 2007 9:59 AM

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.


All times are GMT -5. The time now is 1:58 AM.

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