cisc 3120 c08: inheritance and polymorphism · 2019-12-14 · •java platform class hierarchy...

54
CISC 3120 C08: Inheritance and Polymorphism Hui Chen Department of Computer & Information Science CUNY Brooklyn College 2/26/2018 1 CUNY | Brooklyn College

Upload: others

Post on 10-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

CISC 3120

C08: Inheritance and Polymorphism

Hui Chen

Department of Computer & Information Science

CUNY Brooklyn College

2/26/2018 1CUNY | Brooklyn College

Page 2: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Outline

• Recap and issues

• Project progress? Practice assignments? CodeLab?

• Review guide #1? Test #1?

• Automated unit testing?

• Inheritance

• Access control, getters & setters

• Java platform class hierarchy

• Polymorphism via inheritance

• Type casting

• Some discussion on nested classes

• Assignments

2/26/2018 CUNY | Brooklyn College 2

Page 3: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Recap: Testing

“Programmers are not to be measured by their ingenuity and their logic but by the completeness of their case analysis. “

-- Alan Perlis

2/26/2018 CUNY | Brooklyn College 3

Page 4: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Recap: Unit Testing

• Automated unit tests

• White-box tests

• Test coverage (related to case analysis)

• Separate application logic from tests

• Automate tests

• JUnit

• A unit testing framework for Java

2/26/2018 CUNY | Brooklyn College 4

Page 5: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions?

• Recap and issues

• Project progress?

• Practice assignments?

• CodeLab?

• Review guide #1?

• Test #1?

• Automated unit testing?

2/26/2018 CUNY | Brooklyn College 5

Page 6: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Class and Type

• A class defines a type, and often models a set of entities

• To build a system for managing business at Brooklyn College, we consider

• People, a set of individuals (objects), modeled as a class that defines the set of objects

2/26/2018 CUNY | Brooklyn College 6

People at Brooklyn College

Page 7: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Subtypes

• Some people at Brooklyn are different from the others in some way

• Professors and students are subtypes of Brooklyn College People

2/26/2018 CUNY | Brooklyn College 7

People at Brooklyn College

Professors Students

Page 8: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Type Hierarchy

• Characteristics and behavior

• What are Students and Professors in common?

• What are Students and Professors different?

2/26/2018 CUNY | Brooklyn College 8

People at Brooklyn College

Professors Students

Page 9: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

What’s in common?

• What characteristics (attributes) and behavior (actions) do People at Brooklyn College have in common?

• Characteristics (attributes, fields, or states): name, ID, address, email, phone, …

• Behavior (actions, functions, or methods): change address, apply parking, …

2/26/2018 CUNY | Brooklyn College 9

Page 10: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

What’s Special?

• What’s distinct about students?

• Characteristics (attributes, fields, or states): classes taken, tuition and fees, …

• Behavior (actions, functions, or methods): add class, drop class, pay tuition, …

• What’s distinct about professors?

• Characteristics (attributes, fields, or states): course taught, rank, title, …

• Behavior (actions, functions, or methods): register grade, apply promotion, …

2/26/2018 CUNY | Brooklyn College 10

Page 11: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Inheritance & Type Hierarchy

• A subtype (child) inherits characteristics (attributes) and behavior (actions) of its base type (parent)

2/26/2018 CUNY | Brooklyn College 11

People at Brooklyn College

Professors Students

- Name, ID, address, phone, …

- Change address, apply parking …

- Class taken, …- Add class, …

- Class taught, …- Register grades, …

Page 12: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions

• Concepts

• Type, subtype, class, subclass

• Inheritance

2/26/2018 CUNY | Brooklyn College 12

Page 13: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Terms of Choice

• Terms

• Super type, Super class

• Base type, Base class

• Parent type, parent class

• Child type, child class

• Subtype, subclass

• …

• In Java, we sometimes consider “type” and “class” are slightly different

• In Java, a pure abstract class is called an “interface” (to be discussed in next class)

2/26/2018 CUNY | Brooklyn College 13

Page 14: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions?

• Terms

• Super type, Super class

• Base type, Base class

• Parent type, parent class

• Child type, child class

• Subtype, subclass

• …

2/26/2018 CUNY | Brooklyn College 14

Page 15: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Super Type (Super Class): Personpublic class Person {

protected String name;

protected String id;

protected String address;

public Person(String name, String id, String address) {

this.name = name; this.id = id; …

}

public void changeAddress(String address) { …

}

… }

2/26/2018 CUNY | Brooklyn College 15

Page 16: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Subtype (Subclass): Student

public Student extends Person {

private ArrayList<String> classesTaken;

public Student(String name, String id, String address) {

super(name, id, address);

classesTaken = new ArrayList<String>();

}

public void haveTakenClass(String className) { …

}

public void showClassesTaken() { …

}

…}2/26/2018 CUNY | Brooklyn College 16

Page 17: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Subtype (Subclass): Professor

public class Professor extends Person {

private final static int SABATTICAL_LEAVE_INTERVAL = 7;

private int yearStarted;

public Professor(String name, String id, String address, int yearStarted) {

super(name, id, address);

this.yearStarted = yearStarted;

}

public void applySabbatical(int applicationYear) { …

}

…}

2/26/2018 CUNY | Brooklyn College 17

Page 18: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Control Access to Members

Modifier Class Package Subclass World

public Yes Yes Yes Yes

protected Yes Yes Yes No

(no modifier) Yes Yes No No

private Yes No No No

2/26/2018 CUNY | Brooklyn College 18

… protected String name; …

More

restrictive

Page 19: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Choose Access Control Level

• Goal: you want to reduce the chances your class is being misused. Access level is to help achieve it.

• Use private unless you have a good reason not to.

• Use the most restrictive access level that makes sense for a particular member.

• Avoid public fields except for constants. (Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.)

2/26/2018 CUNY | Brooklyn College 19

Page 20: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Constructors

• Initialize attributes of an object when it is being created (or instantiated)

• Subclass’s constructor

• Java will call the parent class’s default constructor if you do not call one of parent’s constructors explicitly.

• You may explicitly call it via “super(…)”.

2/26/2018 CUNY | Brooklyn College 20

… super(name, id, address); …

Page 21: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Override Methods in Super Class: Methodspublic class Person { ...

public String toString() {

return "Person (name=" + name + ", id=" + id + ", address=" + address + ")";

} …

}

2/26/2018 CUNY | Brooklyn College 21

public class Student extends Person { …

public String toString() {

return "Student (name=" + name + ", id=" + id + ", address=" + address

+ ", coursesTaken=[" + String.join(", ", classesTaken) + "])";

} …

}

Page 22: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Override Methods in Super Class: Example

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Student adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

System.out.println (ben.toString());

System.out.println(adam.toString());

2/26/2018 CUNY | Brooklyn College 22

Person (name=Ben Franklin, id=00124, address=2901 Bedford Ave)

Student (name=Adam Smith, id=00248, address=2902 Bedford Ave, coursesTaken=[])

Page 23: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions

• Inheritance in Java

• Access control of class members

• Constructors

• Overriding methods

• A few other related items

• this, super

2/26/2018 CUNY | Brooklyn College 23

Page 24: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Getters and Setters

• Recall the design principle

• A class should have only a single responsibility and responsible for its own behavior

• Objects interacts with only their methods

• How do we access the private members of a class?

• Getters and setters

• Getters: a method that returns the value of a restricted variable

• Setters: a method that sets the value of a restricted variable

2/26/2018 CUNY | Brooklyn College 24

Page 25: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Getters and Setters: Example

• Observe the getter & setter naming convention

public class Boat {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

2/26/2018 CUNY | Brooklyn College 25

Page 26: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Getters and Setters: Using IDE

• Many IDEs can generate getters and setters for you.

• Examples:

• In the Eclipse IDE, click the “Source” menu, select “Generate Setters and Getters”

2/26/2018 CUNY | Brooklyn College 26

Page 27: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Generating Getters and Setters

2/26/2018 CUNY | Brooklyn College 27

Page 28: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions

• Getters and Setters

• Use IDEs to generate getters and setters

2/26/2018 CUNY | Brooklyn College 28

Page 29: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Polymorphism

• One type appears as and is used like another type

• Example

• A Student object can be used in place of a Person object.

• Inheritance is an approach to realize polymorphism

2/26/2018 CUNY | Brooklyn College 29

Page 30: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Polymorphism: Example 1

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

System.out.println (ben.toString());

System.out.println(adam.toString());

2/26/2018 CUNY | Brooklyn College 30

Person (name=Ben Franklin, id=00124, address=2901 Bedford Ave)

Student (name=Adam Smith, id=00248, address=2902 Bedford Ave, coursesTaken=[])

Page 31: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Polymorphism: Example 2

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

display(ben); display(adam);

2/26/2018 CUNY | Brooklyn College 31

Person (name=Ben Franklin, id=00124, address=2901 Bedford Ave)

Student (name=Adam Smith, id=00248, address=2902 Bedford Ave, coursesTaken=[])

public static void display(Person person) {

System.out.println(person.toString());

}

Page 32: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

How about Other Methods?

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Student adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

adam.haveTakenClass("CISC3120");

display(ben); display(adam);

2/26/2018 CUNY | Brooklyn College 32

Person (name=Ben Franklin, id=00124, address=2901 Bedford Ave)

Student (name=Adam Smith, id=00248, address=2902 Bedford Ave, coursesTaken=[CISC3120])

Page 33: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

How about this example?

• You say, “adam” appears to be a “Student” object.

2/26/2018 CUNY | Brooklyn College 33

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

adam.haveTakenClass("CISC3120");

display(ben); display(adam);

Error: The method haveTakenClass(String) is undefined for the type Person

Page 34: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Type Casting

• You can only invoke the method of declared type, i.e., Person.

2/26/2018 CUNY | Brooklyn College 34

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

((Student)adam).haveTakenClass("CISC3120");

display(ben); display(adam);

Person (name=Ben Franklin, id=00124, address=2901 Bedford Ave)

Student (name=Adam Smith, id=00248, address=2902 Bedford Ave, coursesTaken=[CISC3120])

Page 35: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Actual Type and Declared Type

• Declared type: type at compilation time

• Actual type: type at runtime

• A variable may refer to an object of different type at runtime

• Example: actual and declared types of “ben”, and “adam”?

2/26/2018 CUNY | Brooklyn College 35

Person ben = new Person("Ben Franklin", "00124", "2901 Bedford Ave");

Person adam = new Student("Adam Smith", "00248", "2902 Bedford Ave");

((Student)adam).haveTakenClass("CISC3120");

Page 36: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Type Casting

• Down-casting

• Cast to a subtype

• It is allowed when there is a possibility that it succeeds at run time (e.g., type to be casted to matches actual type)

• In the example, a “Person” object references to a “Student” object, and the down casting is allowed.

• Up-casting

• Cast to a super type

• It is always allowed

2/26/2018 CUNY | Brooklyn College 36

Page 37: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions

• Polymorphism via inheritance in Java

• Type casting in Java

2/26/2018 CUNY | Brooklyn College 37

Page 38: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Design Consideration

• Composition vs. Inheritance

2/26/2018 CUNY | Brooklyn College 38

Page 39: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

More Example: Boat, RowBoat …

• Both examples (Person-Student-Professor and Boat-RowBoat) are in the “Sample Program” repository on Github

2/26/2018 CUNY | Brooklyn College 39

Page 40: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions?

• Inheritance or composition?

2/26/2018 CUNY | Brooklyn College 40

Page 41: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Java Platform Class Hierarchy

• The java.lang.Object class is the ancestor of all classes

• defines and implements behavior common to all classes

• Many classes derive directly from Object

• Other classes derive from some of those classes, and so on, forming a hierarchy of classes.

2/26/2018 CUNY | Brooklyn College 41

Page 42: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

The Objects class

• java.util.Objects

• Static utility methods for operating on objects.

• Examples:

• null-safe or null-tolerant methods for computing the hash code of an object,

• Methods that return a string for an object

• Methods that compare two objects.

2/26/2018 CUNY | Brooklyn College 42

Page 43: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions

• The Java Object and Objects classes

2/26/2018 CUNY | Brooklyn College 43

Page 44: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Nested Class

• Java permits one to define a class within another class. Below are 2 of 4 types:

• Inner class (Non-static nested class)

• Static nested class

2/26/2018 CUNY | Brooklyn College 44

class OuterClass {...class NestedClass { ... }

}

class OuterClass {...static class StaticNestedClass { ... }

}

Page 45: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Using Nested Class

• Logically grouping classes that are only used in one class

• Can increase encapsulation

• Can lead to more readable and maintainable code

2/26/2018 CUNY | Brooklyn College 45

class B {int c;

}class A { // B only used in A

B b = new B();b.c = 2;

}

class A {class B {

int c;}B b = new B();b.c = 2;

}

Page 46: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Inner class

• An inner class is a member of the outer class

• have access to other members of the enclosing class, even if they are declared private.

• An inner class can be declared private, public, protected, or package private.

• However, the outer classes can only be declared public or package private

2/26/2018 CUNY | Brooklyn College 46

Page 47: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Inner Class: Member of Outer Class

• An instance of the inner class is a part of an instance of the outer class

• How about create an object of the inner class

2/26/2018 CUNY | Brooklyn College 47

Page 48: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Inner Class: Member of Outer Class: Examples

• Which one is correct?

2/26/2018 CUNY | Brooklyn College 48

class A {void method() {

B b = new B();}class B { // B only used in A}

}

class A {void method() {

B b = this.new B();}class B { // B only used in A}

}

class A {static void method() {

B b = new B();}class B { // B only used in A}

}

class A {static void method() {

A a = new A();B b = a.new B();

}class B { // B only used in A }

}

Page 49: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Inner Class: Member of Outer Class: Examples

• Which one is correct?

2/26/2018 CUNY | Brooklyn College 49

class A {void method() {

B b = new B();}class B { // B only used in A}

}

class A {void method() {

B b = this.new B();}class B { // B only used in A}

}

class A {static void method() {

B b = new B();}class B { // B only used in A}

}

class A {static void method() {

A a = new A();B b = a.new B();

}class B { // B only used in A }

}

Page 50: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Static Nested Class

• A static nested class is associated with its outer class

• It belongs to the outer class, not to an object of the outer class.

• Behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

2/26/2018 CUNY | Brooklyn College 50

Page 51: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Static Nest Class: Examples

• Which one is correct or wrong?

2/26/2018 CUNY | Brooklyn College 51

class A {void method() {

B b = new B();}static class B { // B only used in A }

}

class A {void method() {

B b = new A.B();}static class B { // B only used in A }

}

class A {static void method() {

B b = new B();}static class B { // B only used in A }

}

class A {static void method() {

B b = new A.B();}static class B { // B only used in A }

}

Page 52: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Static Nest Class: Examples

• Which one is correct or wrong?

2/26/2018 CUNY | Brooklyn College 52

class A {void method() {

B b = new B();}static class B { // B only used in A }

}

class A {void method() {

B b = new A.B();}static class B { // B only used in A }

}

class A {static void method() {

B b = new B();}static class B { // B only used in A }

}

class A {static void method() {

B b = new A.B();}static class B { // B only used in A }

}

Page 53: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Questions?

• Nested classes

• Inner class

• Static nested class

2/26/2018 CUNY | Brooklyn College 53

Page 54: CISC 3120 C08: Inheritance and Polymorphism · 2019-12-14 · •Java platform class hierarchy •Polymorphism via inheritance •Type casting •Some discussion on nested classes

Assignments

• Practice Assignment

• CodeLab

2/26/2018 CUNY | Brooklyn College 54