Basic Classes Subclasses and Inheritance By Greg Butler.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Chapter 10: Introduction to Inheritance
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Classes, Encapsulation, Methods and Constructors
Terms and Rules Professor Evan Korth New York University (All rights reserved)
UML Class Diagram: class Rectangle
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Inheritance using Java
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
What is inheritance? It is the ability to create a new class from an existing class.
CSC 142 Computer Science II Zhen Jiang West Chester University
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Coming up: Inheritance
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
OOP: Encapsulation &Abstraction
Objects as a programming concept
Chapter 3: Using Methods, Classes, and Objects
UML Class Diagram: class Rectangle
Inheritance I Class Reuse with Inheritance
Java Programming Language
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Advanced Java Programming
Applying OO Concepts Using Java
Outline Anatomy of a Class Encapsulation Anatomy of a Method
OOP Aga private institute for computer science 5th grade
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 7 Inheritance.
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.