Results 1 to 12 of 12
  1. #1
    javanoobe is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    let's think about what private and public mean.

    A private variable can only be used inside the class
    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
      }
    }
    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{
      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();
      }
    }
    Test those snippets and see how everything happens.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    In C++, yes. In Java, 'protected' access gives visibility to other classes in the same package (that may not be subclasses), as well as all subclasses.

  4. #4
    javanoobe is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    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.

  6. #6
    javanoobe is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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);
    	}
    }
    Java 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();
    	}
    }
    Thank You

    javanoobe(the name says it all)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    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.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Tolls View Post
    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.

    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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    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.

  10. #10
    javanoobe is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    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.

  12. #12
    javanoobe is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank You. Like some of my other assignments I try to make them more difficult than they have to be.



    javanoobe

Similar Threads

  1. Top level Protected Classes
    By Jithu in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 01:28 PM
  2. protected modifier ? Constructors
    By javanew in forum New To Java
    Replies: 6
    Last Post: 04-06-2010, 11:59 AM
  3. declare class data member as an inteface type
    By everurz in forum Advanced Java
    Replies: 3
    Last Post: 12-21-2009, 01:04 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:53 AM
  5. help with protected method in vector class
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •