Results 1 to 12 of 12
Thread: protected data member
- 04-12-2011, 01:27 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
protected data member
I have a project that requires me to use 'protected' for a few variables.
So I have something like this:
protected int myVariable1;
protected int myVariable2;
protected int myVariable3;
Everything works and passes the information from the parent class to the child classes. My question is how do I check this to demonstrate the protected data can be accessed directly by the child classes? The book we have to use has maybe 50 whole words on the subject of protected and no examples. I understand that protected is a level of security between public and private. I'm have some sort of brain fart on how to check this.
Thank You
javanoobe
- 04-12-2011, 01:42 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
let's think about what private and public mean.
A private variable can only be used inside the class
With the use of protected, you allow the variable to be used outside the original class, if and only if, the class you are using it is a subclass of x.Java Code:class X{ private int x; //has a private variable named x public int y; //has a public variable named x } public class Y extends X{ public static void main(String[] args){ X x = new X(); System.out.println(x.x); //illegal(x is private and can only be used inside class X System.out.println(x.y);//legal, y is public, it can be used outside of class x } }
Test those snippets and see how everything happens.Java Code:class X{ protected x; } class Y extends X{ public void printVars(){ System.out.println(x); } public static void main(String[] args){ Y y = new Y(); y.printVars(); } }
- 04-12-2011, 03:14 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 04-12-2011, 07:09 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Yes I under stand all of that so far and I tried the snippets you posted sunde887 and Eclipse gave me an error for 'x' and I had to add int or string before it to make it work. Now since I'm not completely sure as to what all I need to do to apply the 'protected' to a variable I need some way to check this to see if I implemented it correctly. Here is what I have:
//my vehicle class
public class Vehicle
{
protected int wheels;
protected int mpg;
//Constructor
public Vehicle(int wheels2, int mpg2)
{
wheels = wheels2;
mpg = mpg2;
}
//get statements
public int getWheels()
{
return wheels;
}
public double getMpg()
{
return mpg;
}
//toString display method
public String toString()
{
String vehicleString = "Number of wheels is: " + wheels + " mpg is: " + mpg;
return vehicleString;
}
}
************************************************** ********
//my car class
public class Car extends Vehicle
{
public Car(int wheels, int mpg)
{
super(wheels, mpg);
}
}
************************************************** ********
my motorcycle class
public class MotorCycle extends Vehicle
{
public MotorCycle(int wheels, int mpg)
{
super(wheels, mpg);
}
}
************************************************** ********
//the application
public class UseVehicle
{
public static void main(String[] args)
{
//instantiates car and motorcycle
Car myCar = new Car(4, 27);
MotorCycle myMotorCycle = new MotorCycle(2, 55);
//calls display method
System.out.println(myCar.toString());
System.out.println(myMotorCycle.toString());
}
}
I have a sneaking suspicion I'm not implementing 'protected' correctly for the variables.
Thank You
javanoobe
P.S. Sorry didn't notice the code tag option until after I posted.Last edited by javanoobe; 04-12-2011 at 07:33 PM. Reason: oops
- 04-13-2011, 08:50 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You've implemented it correctly.
It's just you're not using them as protected, they could just as easily be private and your code would still work. Of course that would actually be good design, but since your goal seems to be show protected working then I would create a toString() method in the subclasses that printed the subclass name, and both parent attributes.
- 04-13-2011, 08:59 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
OK. I set a value to one of the variables in my Vehicle class then put a print statement in and it worked. I'm still not sure this is right. What I have to do is have a constructor in my Vehicle class that requires a value for each field. I believe I have that. Then I need to set the values in the Car and motorcycle class, I believe I did that. But if the point of protected is to access the variable directly form the parent class, I think the constructor in Vehicle is still passing the variables. If this is the case how do I get the variable from Vehicle and then pass the value back to the constructor in Vehicle? This is my first programing class and I'm still trying to understand things.
Java Code:public class Vehicle { protected int wheels; protected int mpg; //Constructor public Vehicle(int wheels2, int mpg2) { wheels = wheels2; mpg = mpg2; } //toString display method public String toString() { String vehicleString = "Number of wheels is: " + wheels + " mpg is: " + mpg; return vehicleString; } }Java Code:public class Car extends Vehicle { public Car(int wheels, int mpg) { super(6, 90); } public void printCars() //testing { System.out.print("testing: " + wheels); } }Java Code:public class MotorCycle extends Vehicle { public MotorCycle(int wheels, int mpg) { super(3, 80); } }Thank YouJava Code:public class UseVehicle { public static void main(String[] args) { //instantiates car and motorcycle //I put garbage values in demonstrate the values are being set by the other 2 classes Car myCar = new Car(0, 0); MotorCycle myMotorCycle = new MotorCycle(0, 0); //calls display method System.out.println(myCar.toString()); System.out.println(myMotorCycle.toString()); myCar.printCars(); } }
javanoobe(the name says it all)
- 04-14-2011, 08:19 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Sorry, I have no idea what this exercise is intended to show, so I can't say what it is you are supposed to do.
- 04-14-2011, 09:51 AM #8
from inside the same package protected works like public. but from an other package? the protected variable is not yet visible if you make an instance from the class with protected but if you subclass the class with protected you will have access. none of the examples above show the 'meccano' from outside the package.Last edited by j2me64; 04-14-2011 at 09:55 AM.
- 04-14-2011, 10:02 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I know all that.
I have been doing this for over 10 years after all.
I was saying I didn't understand what the OP is actually after.
- 04-14-2011, 10:38 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
OK, these are the instructions for this project.
Create a Vehicle class. Include protected(2 at least) data members of your choice.
Include a constructor that requires a value for each data field and a toString() method that returns a String containing a complete description of the vehicle. Save file as Vehicle.java.
Create two classes Car and Motorcycle that decend from Vehicle.
Supply each with a constructor that sets the Vehicle data fields with values you choose.
Save files as Car.java and Motorcycle.java.
Create an application that instatiates one Car and one Motorcycle.
call the toString() method with each object and display the results.
Save the file as UseVehicle.java.
I think I've done all of that. However, because I'm using the protected word I don't think I'm doing it right. I would think the Car and Motorcycle classes have to access the variables directly from Vehicle and I don't think that's happening with the code I have.
Thank you
javanoobe
- 04-15-2011, 08:43 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
OK.
Looks like you did everything that was asked for in the code in post #4. Stick with that code.
I'm not sure why you've been asked to make the attributes protected, but it could be something you use later on in the course.
- 04-15-2011, 09:09 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Top level Protected Classes
By Jithu in forum New To JavaReplies: 2Last Post: 10-06-2010, 01:28 PM -
protected modifier ? Constructors
By javanew in forum New To JavaReplies: 6Last Post: 04-06-2010, 11:59 AM -
declare class data member as an inteface type
By everurz in forum Advanced JavaReplies: 3Last Post: 12-21-2009, 01:04 PM -
Error: Cannot access protected member long getTimeInMillis() in class Calendar
By cachi in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:53 AM -
help with protected method in vector class
By katie in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 10:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks