9/10/2018 programming data structures inheritance · introduction to inheritance (1 of 2)...

50
9/10/2018 Programming Data Structures 1 Inheritance

Upload: others

Post on 14-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

9/10/2018

Programming Data Structures

1

Inheritance

Page 2: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Email me if the office door is closed

2

Page 3: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Introduction to Arrays

• An array is a data structure used to process a collection of data that is all of the same type• An array behaves like a numbered list of variables with a

uniform naming mechanism

• It has a part that does not change: the name of the array

• It has a part that can change: an integer in square brackets

• For example, given five scores:

3

Page 4: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved

Creating and Accessing Arrays (1 of 5)

• An array that behaves like this collection of variables, all of type double, can be created using one statement as follows:

• Or using two statements:

• The first statement declares the variable score to be of the array type double[]

• The second statement creates an array with five numbered variables of type double and makes the variable score a name for the array

4

Page 5: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Creating and Accessing Arrays (2 of 5)

• The individual variables that together make up the array are called indexed variables• They can also be called subscripted variables or elements

of the array

• The number in square brackets is called an index or subscript

• In Java, indices must be numbered starting with 0, andnothing else

5

Page 6: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

two_dimensional_array[0] = new int[5];

two_dimensional_array[1] = new int[5];

two_dimensional_array[2] = new int[5];

Array – 2 Dimensional

Declaration:

6

int[] one_dimensional_array = new int[3];

int[][] two_dimensional_array = new int[3][5];

int[][] two_dimensional_array = new int[3][];

Download Array1.java from GitHub and run the main method.

Page 7: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Run the program

1. Create a class, name as “Array1” (case-sensitive)

2. Go to GitHub.com,

assignment02-github_userid/Array1.java• Copy and paste the content to the Array1 class you just

created

3. Run the main method, and see the outputs

7

For those who didn’t attend the class, get the assignment repository at:

https://classroom.github.com/a/8wyuZOMY

Page 8: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Change the program

Check line 9 and line 10 in your Array1 java file.

And make the corresponding modifications.

8

Hint:For Array A, int[] A = new int[10]The length of A can be accessed as: A.length

More example: https://alvinalexander.com/java/java-array-length-example-length-method

Page 9: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Array

9

int[][] two_dimensional_array = new int[3][];

two_dimensional_array[0] = new int[1];

two_dimensional_array[1] = new int[3];

two_dimensional_array[2] = new int[5];

Page 10: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

How do you iterate the two-dimensional array?

10

Download Array2.java from GitHub and run the main method.

1. Create a class, name as “Array2” (case-sensitive)

2. Go to GitHub.com,

assignment02-github_userid/Array2.java• Copy and paste the content to the Array2 class you just

created

Run the main method, and the output should be:

[[0, 0, 0, 0, 0], [0], [0, 0]]

Page 11: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

The assignment is:

Iterating through the 2-dimensional array and set all the elements in the array to 1

So your submission’s output should be:

[[1, 1, 1, 1, 1], [1], [1, 1]]

11

How do you iterate the two-dimensional array?

Assignment!!

Page 12: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Grading rules (0.25)Deadline: Tuesday 9/11 12pm

12

Download Array2.java from GitHub and run the main method.

0.1: System prints:[[1, 1, 1, 1, 1], [1], [1, 1]]

0.15: do not use constants in the loopsfor example, there is no ‘3’, ‘5’, ‘1’, ‘2’ in your loop

statements

15 min in class

Page 13: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Inheritance

13

Page 14: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Introduction to Inheritance (1 of 2)

• Inheritance is one of the main techniques of object-oriented programming (OOP)

• Using this technique, a very general form of a class is first defined and compiled, and then more specialized versions of the class are defined by adding instance variables and methods• The specialized classes are said to inherit the methods and

instance variables of the general class

14

Page 15: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Example: coffee

15

public class

Coffee

public class

SingleShot

public class

EspressoMacchiato

How about copy and paste similar code into different classes?

Page 16: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Example: coffee

16

public class

ShortShot

public class

EspressoMacchiato

public class

SingleShot

public class

LongShot

public class

con Pannapublic class

Cafe Breve

public class

Cappuccino

public class

Coffee

Page 17: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Introduction to Inheritance (2 of 2)

• Inheritance is the process by which a new class is created from another class• The new class is called a derived class

• The original class is called the base class

• A derived class automatically has all the instance variables and methods that the base class has, and it can have additional methods and/or instance variables as well

• Inheritance is especially advantageous because it allows code to be reused, without having to copy it into the definitions of the derived classes

17

Page 18: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Classes (1 of 7)

•When designing certain classes, there is often a natural hierarchy for grouping them• In a record-keeping program for the employees of a

company, there are hourly employees and salaried employees

• Hourly employees can be divided into full time and part time workers

• Salaried employees can be divided into those on technical staff, and those on the executive staff

18

Page 19: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Display 7.1 A Class Hierarchy (P. 461)

19

Page 20: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Introduction to Inheritance (2 of 2)

• Inheritance is the process by which a new class is created from another class• The new class is called a derived class

• The original class is called the base class

• A derived class automatically has all the instance variables and methods that the base class has, and it can have additional methods and/or instance variables as well

• Inheritance is especially advantageous because it allows code to be reused, without having to copy it into the definitions of the derived classes

20

Page 21: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Classes

•All employees share certain characteristics in common• All employees have a name and a hire date• The methods for setting and changing names and hire

dates would be the same for all employees

• Some employees have specialized characteristics• Hourly employees are paid an hourly wage, while salaried

employees are paid a fixed wage• The methods for calculating wages for these two different

groups would be different

21

Page 22: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Read the example source code

1. Go to GitHub.com,

assignment02-github_userid/Employee.java

22

Page 23: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Classes

•Within Java, a class called Employee can be defined that includes all employees

• This class can then be used to define classes for hourly employees and salaried employees• In turn, the HourlyEmployee class can be used

to define a PartTimeHourlyEmployee class, and so forth

23

Page 24: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Read the example source code

1. Go to GitHub.com,

assignment02-github_userid/HourlyEmployee.java

24

Page 25: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Classes (5 of 7)

• Since an hourly employee is an employee, it is defined as a derived class of the class Employee• A derived class is defined by adding instance variables and

methods to an existing class

• The existing class that the derived class is built upon is called the base class

• The phrase extends BaseClass must be added to the derived class definition:public class HourlyEmployee extends Employee

25

Page 26: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Classes (6 of 7)

• When a derived class is defined, it is said to inherit the instance variables and methods of the base class that it extends• Class Employee defines the instance variables name and hireDate in its class definition

• Class HourlyEmployee also has these instance variables, but they are not specified in its class definition

• Class HourlyEmployee has additional instance variables wageRate and hours that are specified in its class definition

26

Page 27: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Classes (7 of 7)

• Just as it inherits the instance variables of the class Employee, the class HourlyEmployee inherits all of its methods as well• The class HourlyEmployee inherits the methods getName, getHireDate, setName, and setHireDate from the class Employee

• Any object of the class HourlyEmployee can invoke one of these methods, just like any other method

27

Page 28: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Derived Class (Subclass)

• A derived class, also called a subclass, is defined by starting with another already defined class, called a base class or superclass, and adding (and/or changing) methods, instance variables, and static variables• The derived class inherits all the public methods, all the public and private

instance variables, and all the public and private static variables from the base class

• The derived class can add more instance variables, static variables, and/or methods

28

Page 29: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Inherited Members

• A derived class automatically has all the instance variables, all the static variables, and all the public methods of the base class• Members from the base class are said to be inherited

• Definitions for the inherited variables and methods do not appear in the derived class• The code is reused without having to explicitly copy it, unless the creator of

the derived class redefines one or more of the base class methods

29

Page 30: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Parent and Child Classes

•A base class is often called the parent class• A derived class is then called a child class

• These relationships are often extended such that a class that is a parent of a parent . . . of another class is called an ancestor class• If class A is an ancestor of class B, then class B can be

called a descendent of class A

30

Page 31: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Terminology

31

Parent Class = Base class = SuperclassChild Class = Derived = Subclass

Page 32: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Pitfall: The Terms “Subclass” and “Superclass”

• The terms subclass and superclass are sometimes mistakenly reversed• A superclass or base class is more general and inclusive, but less

complex

• A subclass or derived class is more specialized, less inclusive, and more complex• As more instance variables and methods are added, the number of

objects that can satisfy the class definition becomes more restricted

32

Page 33: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Overriding a Method Definition

• Although a derived class inherits methods from the base class, it can change or override an inherited method if necessary• In order to override a method definition, a new definition of the

method is simply placed in the class definition, just like any other method that is added to the derived class

33

public String toString( ) in both Employee.java and hourlyEmployee.java

Page 34: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Changing the Return Type of an Overridden Method• Ordinarily, the type returned may not be changed when overriding a

method

• However, if it is a class type, then the returned type may be changed to that of any descendent class of the returned type

• This is known as a covariant return type• Covariant return types are new in Java 5.0; they are not allowed in earlier

versions of Java

34

Page 35: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved

Covariant Return Type• Given the following base class:

• The following is allowed in Java 5.0:

35

Page 36: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Changing the Access Permission of an Overridden Method (1 of 2)

• The access permission of an overridden method can be changed from private in the base class to public (or some other more permissive access) in the derived class

• However, the access permission of an overridden method can not be changed from public in the base class to a more restricted access permission in the derived class

36

Page 37: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved

Changing the Access Permission of an Overridden Method (2 of 2)

• Given the following method header in a base case:

• The following method header is valid in a derived class:

• However, the opposite is not valid

• Given the following method header in a base case:

• The following method header is not valid in a derived class:

37

Page 38: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Pitfall: Overriding Versus Overloading

• Do not confuse overriding a method in a derived class with overloading a method name• When a method is overridden, the new method definition

given in the derived class has the exact same number and types of parameters as in the base class

• When a method in a derived class has a different signature from the method in the base class, that is overloading

• Note that when the derived class overloads the original method, it still inherits the original method from the base class as well

38

Page 39: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

The FinalModifier

• If the modifier final is placed before the definition of a method, then that method may not be redefined in a derived class

• It the modifier final is placed before the definition of a class, then that class may not be used as a base class to derive other classes

39

Page 40: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved

The Super Constructor (1 of 3)

• A derived class uses a constructor from the base class to initialize all the data inherited from the base class

• In order to invoke a constructor from the base class, it uses a special syntax:

• In the above example, p2); super(p1, is a call to the base

class constructor

40

Page 41: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

The Super Constructor (2 of 3)

• A call to the base class constructor can never use the name of the base class, but uses the keyword super instead

• A call to super must always be the first action taken in a constructor definition

• An instance variable cannot be used as an argument to super

41

Page 42: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

The Super Constructor (3 of 3)

• If a derived class constructor does not include an invocation of super, then the no-argument constructor of the base class will automatically be invoked• This can result in an error if the base class has not defined a no-argument

constructor

• Since the inherited instance variables should be initialized, and the base class constructor is designed to do that, then an explicit call to super should always be used

42

Page 43: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

The This Constructor (1 of 3)

•Within the definition of a constructor for a class, this can be used as a name for invoking another constructor in the same class• The same restrictions on how to use a call to super apply

to the this constructor

43

Page 44: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved

The This Constructor (2 of 3)

44

Page 45: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Tip: An Object of a Derived Class has More than One Type (1 of 2)

•An object of a derived class has the type of the derived class, and it also has the type of the base class

•More generally, an object of a derived class has the type of every one of its ancestor classes• Therefore, an object of a derived class can be assigned to

a variable of any ancestor type

45

Page 46: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Tip: An Object of a Derived Class has More than One Type (2 of 2)

•A derived class object can be used anyplace that an object of any of its ancestor types can be used

•However, that this relationship does not go the other way• An ancestor type can never be used in place of one of its

derived types

46

Page 47: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

In-class assignment (Textbook, Page 509 –510, Programming Projects 3): 1.75Code is in the GitHub (check the last 2 pages). The following is some code designed by J. Hacker for a video game. There is an Alien class to represent a monster and an AlienPack class that represents a band of aliens and how much damage they can inflict. (Check out your email for the GitHub repository.)The code is not very object oriented and does not support information hiding in the Alien class. Rewrite the code so that inheritance is used to represent the different types of aliens instead of the “type” parameter. This should result in deletion of the “type” parameter. Also rewrite the Alien class to hide the instance variables and create a getDamage method for each derived class that returns the amount of damage the alien inflicts. Finally, rewrite the calculateDamage method to use getDamage and write a main method that tests the code.

53

Page 48: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Hint: the class diagram

54

Alien

SnakeAlien MarshmallowAlien OgreAlien

AlienPack

Page 49: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Download the code files:

1. Go to GitHub.com, assignment02-github_userid/2. Download the following files:

a) Alien.javab) AlienPack.javac) Main.java

https://sjiang1.github.io/classes/java2/18fall/00_GitHubGuide.pdfStep 7 downloading a file

55

Page 50: 9/10/2018 Programming Data Structures Inheritance · Introduction to Inheritance (1 of 2) •Inheritance is one of the main techniques of object-oriented programming (OOP) •Using

Grading rules (1.75)

0.25: You have the three subclasses defined but the main method does not have the correct output

0.5: (1) the main method outputs “Total pack damage = 17”

** You cannot change the main method

1: satisfies (1) and the three classes: OgreAlien, SnakeAlien, MarshmallowAlien are subclasses of Alien class.

56