Presentation on theme: "Basic Classes Subclasses and Inheritance By Greg Butler."— Presentation transcript:
Basic Classes Subclasses and Inheritance By Greg Butler
Purpose In the past lesson we learned that we could put several classes in the same package, but that only one could be “public.” We also learned how to call a class’ public methods from another class and how to access its public variables from another class. In this lesson we will explore extending a class to create subclasses, public and private methods and variables, and class and instance variables and methods. In the next lesson we will learn to override methods and variables; explore class variables in more detail, learn about abstract classes, explain the concepts of instantiation, destruction, finalization, and garbage collection; and use interfaces.
Objectives Upon completion of this lesson, the student must be able to, when presented with a multiple choice test, short answer test, or programming problem: Demonstrate a clear understanding of the difference between private and public methods and variables and be able to properly use them in a computer program. Demonstrate a clear understanding of the difference between class and instance methods and variables and be able to properly use them in a computer program. Demonstrate the ability to implement classes and associated subclasses. Demonstrate the ability to implement public and protected methods to access private class methods and variables from within the current package.
Access Modifiers Public,Private, Protected, and Package We can control which methods and variable are "visible" (can be accessed) from outside the class in which they are declared. All variables and methods defined in a class, are accessible from within that class. We can control access to methods and variables using access modifiers. Accessible toMember Visibility publicprotectedprivatepackage Defining ClassYes Class in Same Package Yes NoYes Subclass in a Different Package Yes No Non-subclass in a Different Package YesNo This chart from Flanagan, D. (1999). Java in a Nutshell. (3rd Ed.), O'Reilly p 108. As a general rule, we tend to make class and instance variables "private", and then use public methods if other classes to provide access to them. This approach supports data encapsulation and abstraction Subclasses can access all non- private instance variables and methods, if defined in the same package as the superclass. (We'll leave the case of a different package to a later discussion). Subclasses actually inherit all methods and variable from the superclass (e.g. memory is allocated), but not all are accessible.
Class and Instance Variables and Methods static Class fields and methods are associated with the class in which they are defined, –"static" designates a class field or method –They are not associated with the instances of that class and cannot access instance methods and fields –They can be used without creating a class instance Instance methods and fields are associated with the instances of the class and, therefore, can only be used if an instance of the class exists.
Subclasses and Inheritance: Example Overview Employee private empName(String) private hireDate (String) protected SetHireDate (String) public String GetHireDate () protected SetEmpName (String) public String GetEmpName () SalariedEmployee private monthlyPayRate double private vacationAvailable float public SetMonthlyPayRate (double) public double GetMonthlyPayRate () public EnterVacationUsed (float) private CalculateVacation() public float GetVacationAvailable() public PrintPayStub() HourlyEmployee private hourlyPayRate double protected SetHourlyPayRate (double) public double GetHourlyPayRate () public PrintPayStub() In our example, we are designing a piece of code to manage our employee records. We determined that we have two subclasses of employees. The critical elements of each class is depicted below. Each is discussed
Subclasses and Inheritance: Example- Employee Class Employee private empName(String) private hireDate (String) protected SetHireDate (String) public String GetHireDate () protected SetEmpName (String) public String GetEmpName () Common to each of the employees is their name and hire date. It is an integer value YYDDD The hire date and name can be set only through the protected method.This methods can be accessed by a class or subclass in the same package (or a subclass defined in another package). We are following a general set of rules that say we should err on the side of being more restrictive in selecting the access modifier for our method. The hire date and name can be retrieved only through the public method We have also followed the general rule of declaring variables (fields) as private and accessing them through methods. Sample Code for Employee class
Subclasses and Inheritance: Example- Salaried Employee Subclass SalariedEmployee private monthlyPayRate double private vacationAvailable float public SetMonthlyPayRate (double) public double GetMonthlyPayRate () public EnterVacationUsed (float) private CalculateVacation() public float GetVacationAvailable() public PrintPayStub() Our salaried employees are a type of employees that share a hire date with other types of employees, but have other attributes that set them apart. Those differences that are relevant to our problem are the basis for our SalariedEmployee subclass. Salaried employees have a fixed monthly pay rate. Salaried employees get paid vacations. Currently the amount of available vacation is based on the days that have passed since they were hired multiplied by.15, minus the days used. Salaried employee pay stubbs show their name, monthly pay, and remaining vacation. Sample Code for the SalariedEmployee subclass
Subclasses and Inheritance: Example- HourlyEmployeeSubclass HourlyEmployee private hourlyPayRate double protected SetHourlyPayRate (double) public double GetHourlyPayRate () public PrintPayStub() Hourly employees share a hire date with other types of employees, but are set apart because they are paid hourly. Hourly employee pay stubs show their name and hourly pay rate.
Summary "extends" is the reserve word used in creating a subclass. –The new subclass inherits and can use all of the non-private methods and fields (a.k.a attributes and class variables) of the superclass. –Generally we make the access control on class fields private, and then use public or protected methods to set and retrieve their values. "private" methods and fields can only be accessed from within the class in which they are defined. "public" means that they can be accessed directly from another class, even if it is not in the same package. "protected" methods and fields can be accessed by any other class in the same package or by sub-classes (who need not reside in the same package). "static" designates a class variable or method that can be used without creating a class instance. Instance variables or methods (class scope, but no "static" reserve word) can only be used if an instance of the class exists. In our sample code, all class scope variables are instance variables.