Confused about sub class and class access.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Confused about sub class and class access.

  1. #1
    Join Date
    Mar 2006
    Posts
    37

    Confused about sub class and class access.

    The book I am reading has now confused me. my first question is when you extend a class, the subclass will have all the same methods and instance variables as the super class right?

    My book wrote that one of the methods does not have access to the private fields of the superclass. How is that possible? I can post the program if that can help.

    My question my be confusing becuase I am confused my self.

  2. #2
    Join Date
    Apr 2006
    Location
    Israel
    Posts
    5
    The private identifier allows access only to functions from the same class.
    Here is a visibility chart found in the Sun website:

    Code:
    Specifier      | Class  |  Package  |  Subclass |  World
    private        |    Y    |      N       |       N      |     N
    no specifier  |    Y    |      Y       |       N      |     N
    protected    |    Y    |      Y       |       Y      |     N
    public         |    Y    |       Y      |        Y     |      Y
    Or in other words:
    To have a subclass able to access the superclass method or modifier you need to have it public(visible for all the classes you have in the applet) or protected (visible to all classes int the same package and subclasses-regrdless of package)
    Last edited by Mefisto; 04-01-2006 at 02:20 AM.

  3. #3
    Join Date
    Mar 2006
    Posts
    37
    Well I was playing around with the program and I was able to access one of the Employee's class instance fields with the manager class.

    Here is the code for the program.

    Employee Class:
    Code:
    import java.util.*;
    
    class Employee
    {
    	public Employee(String aName, double aSalary, int year, int month, int day)
    	{
    		name = aName;
    		salary = aSalary;
    		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    		hireDay = calendar.getTime();
    		
    	}
    	
    	public void raiseSalary(double byPercent)
    	{
    		double raise = this.salary * byPercent / 100;
    		this.salary += raise;
    	}
    	
    	public double getSalary()
    	{
    		return salary;
    	}
    	
    	public Date getHireDay()
    	{
    		return hireDay;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	public int getID()
    	{
    	
    		return id;
    	}
    	
    	private int id;
    	private static int nextid = 1;
    	private final String name;
    	private double salary;
    	private Date hireDay;
    	
    	{
    		id = nextid;
    		nextid +=2;
    	}
    }

    Manager Class:
    Code:
    import java.util.*;
    
    class Manager extends Employee
    {
    	/**
    		@param aName The employee's name
    		@param aSalary the salary
    		@param year the hire year
    		@param month the hire month
    		@param day the hire day
    	*/
    	
    	public Manager(String aName, double aSalary, int year, int month, int day)
    	{
    		super(aName, aSalary, year, month, day);
    		bonus = 0;
    	}
    	
    	public double getSalary()
    	{
    		double baseSalary = super.getSalary();
    		return baseSalary + bonus;
    	}
    	
    	public void setBonus(double b)
    	{
    		bonus = b;
    	}
    	
    	private double bonus;
    }
    The program
    Code:
    import java.util.*;
    
    public class StaffTest
    {
    	public static void main(String myArgs[])
    	{
    		
    		// Construct Manager Object
    		Manager boss = new Manager("Josh Stephens", 80000,2003,11,24);
    		boss.setBonus(5000);
    	
    		// Fill Staff Array
    		Employee[] staff = new Employee[3];
    		staff[0] = boss;
    		staff[1] = new Employee("John Smith", 27000,2003,9,9);
    		staff[2] = new Employee("Tommy Tester", 40000, 2001, 3,15);
    	
    		// Print out information
    		for(Employee e: staff)
    			{
    				System.out.println("name - " + e.getName() + " , Salary - $" + e.getSalary());
    			}
    		}
    }
    If I change The getSalary() method of the manager class to be just
    Code:
    getSalary()
    {
      return salary + bonus;
    }
    The program Errors out. But Why can I access the name field? They are both defined the same way.

  4. #4
    Join Date
    Mar 2006
    Posts
    37

    Figured it out

    Thanks for the help earlier. I figured it out. Subclasses inherits the private fields but they must still be accessed through the use of the superclass's methods. I realized that they super because the where overriding the method call in the manager class, so it would have ended up in a continous loop.

    Between your chart and me tearing up the program many many time I was able to figure it out. No wonder they say their is no substitute for expierence.

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    no subclasses do not inherit private fields, thats why you can only access them from the get and set methods.

  6. #6
    Join Date
    Mar 2006
    Posts
    37
    I see, so does the manager class then contain four fields, the name field, hireday field, the salary field, and bounus?

  7. #7
    Join Date
    Apr 2006
    Location
    Israel
    Posts
    5

    I think I've encountered this specific exersice in school

    And it didn't work...
    Because the fields like "name" are private

  8. #8
    Join Date
    Mar 2006
    Posts
    37
    I was able to make it work with the code but I was trying to figure out why I had to use super. I have since re read my book several times and I have a better understanding than before.

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  2. Replies: 4
    Last Post: 10-22-2005, 11:39 PM
  3. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  4. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM
  5. Replies: 0
    Last Post: 04-26-2001, 10:01 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center