Top Banner
Inheritance Anastasia Rashtchian
113

OOP Inheritance

Mar 20, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OOP Inheritance

Inheritance

Anastasia Rashtchian

Page 2: OOP Inheritance

A Bit About Me….

Computer Consultant, Trainer and Educator

Master of Science – Computer Science

Illinois Institute of Technology

• Artificial Intelligence and Expert Systems

• Current Interest: Machine Learning

Master of Education – Education, Policy, Organization, Leadership

University of Illinois – Urbana-Champaign

• eLearning in Higher Education

• Current Interest: Automated Adaptive Learning Systems

Page 3: OOP Inheritance

Let’s Review

Page 4: OOP Inheritance

Let’s Review

• What are some recent topics you have covered?

Page 5: OOP Inheritance

Let’s Review

• What are some recent topics you have covered?

• What are some programs you created?

Page 6: OOP Inheritance

Learning Objectives

• To understand the Object Oriented programming concept - inheritance

Page 7: OOP Inheritance

Learning Objectives

• To understand the Object Oriented programming concept - inheritance

• To identify class hierarchies, single and multiple inheritance

Page 8: OOP Inheritance

Learning Objectives

• To understand the Object Oriented programming concept - inheritance

• To identify class hierarchies, single and multiple inheritance

• To define the syntax of inheritance in Java

Page 9: OOP Inheritance

Learning Objectives

• To understand the Object Oriented programming concept - inheritance

• To identify class hierarchies, single and multiple inheritance

• To define the syntax of inheritance in Java

• To analyze super and sub classes

Page 10: OOP Inheritance

Learning Objectives

• To understand the Object Oriented programming concept - inheritance

• To identify class hierarchies, single and multiple inheritance

• To define the syntax of inheritance in Java

• To analyze super and sub classes

• To examine the effect of inheritance on encapsulation and constructors

Page 11: OOP Inheritance

What is Inheritance?

Page 12: OOP Inheritance

What is Inheritance?

Inheritance is a relationship between two or more classes where derived classes inherit behaviours and attributes of pre-existing (base) classes.

Page 13: OOP Inheritance

What is Inheritance?

Inheritance is a relationship between two or more classes where derived classes inherit behaviours and attributes of pre-existing (base) classes.

Page 14: OOP Inheritance

What are some Benefits of Inheritance?

• Intended to help reuse existing code with little or no modification.

Page 15: OOP Inheritance

• Intended to help reuse existing code with little or no modification.

What are some Benefits of Inheritance?

Page 16: OOP Inheritance

• Intended to help reuse existing code with little or no modification.

• Easy to implement real world models

What are some Benefits of Inheritance?

Page 17: OOP Inheritance

• Intended to help reuse existing code with little or no modification.

• Easy to implement real world models

• Is of a transitive nature

What are some Benefits of Inheritance?

Page 18: OOP Inheritance

What is a Class Hierarchy?

Page 19: OOP Inheritance

What is a Class Hierarchy?

• The class hierarchy is usually drawn as an inverted (upside-down) tree.

Page 20: OOP Inheritance

What is a Class Hierarchy?

• The class hierarchy is usually drawn as an inverted (upside-down) tree.

Page 21: OOP Inheritance

What is a Class Hierarchy?

• The class hierarchy is usually drawn as an inverted (upside-down) tree.

• The tree can have any number of levels.

Page 22: OOP Inheritance

What is a Class Hierarchy?

• The class hierarchy is usually drawn as an inverted (upside-down) tree.

• The tree can have any number of levels.

• The class at the top (base) of the inverted tree is called the root class.

Page 23: OOP Inheritance

What is a Class Hierarchy?

• The class hierarchy is usually drawn as an inverted (upside-down) tree.

• The tree can have any number of levels.

• The class at the top (base) of the inverted tree is called the root class.

Root Class

Page 24: OOP Inheritance

Java Class Hierarchy

• In Java, the root class is called Object.

Page 25: OOP Inheritance

Java Class Hierarchy

• In Java, the root class is called Object.

• Every class is a descendant, direct or indirect, of the Object class.

Page 26: OOP Inheritance

Java Class Hierarchy

• In Java, the root class is called Object.

• Every class is a descendant, direct or indirect, of the Object class.

• Every class you use or write inherits the instance methods of Object.

Page 27: OOP Inheritance

Java Class Hierarchy

• In Java, the root class is called Object.

• Every class is a descendant, direct or indirect, of the Object class.

• Every class you use or write inherits the instance methods of Object.

Page 28: OOP Inheritance

Java Class Hierarchy

• In Java, the root class is called Object.

• Every class is a descendant, direct or indirect, of the Object class.

• Every class you use or write inherits the instance methods of Object.

• The Object class is defined in the java.lang package

Page 29: OOP Inheritance

What Would You Do?

Page 30: OOP Inheritance

What Would You Do?

How would you know what attributes and methods to define for the various levels in the hierarchy?

Page 31: OOP Inheritance

Inheritance Concepts - Terms

Page 32: OOP Inheritance

Inheritance Concepts - Terms

• OOP languages provide specific mechanisms for defining inheritance relationships between classes.

Page 33: OOP Inheritance

Inheritance Concepts - Terms

• OOP languages provide specific mechanisms for defining inheritance relationships between classes.

• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.

Page 34: OOP Inheritance

Inheritance Concepts - Terms

• OOP languages provide specific mechanisms for defining inheritance relationships between classes.

• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.

• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.

Page 35: OOP Inheritance

Inheritance Concepts - Terms

• OOP languages provide specific mechanisms for defining inheritance relationships between classes.

• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.

• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.

Page 36: OOP Inheritance

Inheritance Concepts - Terms

• OOP languages provide specific mechanisms for defining inheritance relationships between classes.

• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.

• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.

• What attributes are inherited?

Page 37: OOP Inheritance

Inheritance Concepts - Terms

• OOP languages provide specific mechanisms for defining inheritance relationships between classes.

• (Derived) (Child) (Sub) Class - a class that inherits characteristics of another class.

• (Base) (Parent) (Super) Class - a class from which characteristics are inherited by one or more other classes.

• A derived class inherits data and function members from ALL of its base classes.

Page 38: OOP Inheritance

Examples of Single and Multiple Inheritances

Each Java class has one

(and only one) superclass.

Page 39: OOP Inheritance

Examples of Single and Multiple Inheritances

Each Java class has one

(and only one) superclass.

C++ allows for multiple

inheritance

Page 40: OOP Inheritance

Examples of Single and Multiple Inheritances

Each Java class has one

(and only one) superclass.

C++ allows for multiple

inheritance

Classes higher in the

hierarchy are more

general and more

abstract

Page 41: OOP Inheritance

Examples of Single and Multiple Inheritances

Each Java class has one

(and only one) superclass.

C++ allows for multiple

inheritance

Classes higher in the

hierarchy are more

general and more

abstract

Classes lower in the

hierarchy are more

specific and concrete

Page 42: OOP Inheritance

Inheritance can be continuous…

42

Page 43: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

43

Page 44: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

The derived class can act as a base class so another class can inherit from it

44

Page 45: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

The derived class can act as a base class so another class can inherit from it

All features of the base class are available in the derived class

45

Page 46: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

The derived class can act as a base class so another class can inherit from it

All features of the base class are available in the derived class

46

If you change the base class, all derived classes also change

Page 47: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

The derived class can act as a base class so another class can inherit from it

All features of the base class are available in the derived class

47

If you change the base class, all derived classes also changeAny changes in the derived class do not change the base class

Page 48: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

The derived class can act as a base class so another class can inherit from it

All features of the base class are available in the derived class

Also, the additional features in the derived class are not available in the base class

48

If you change the base class, all derived classes also changeAny changes in the derived class do not change the base class

Page 49: OOP Inheritance

Inheritance can be continuous…Derived class can inherit from a base class

The derived class can act as a base class so another class can inherit from it

All features of the base class are available in the derived class

Also, the additional features in the derived class are not available in the base class

49

If you change the base class, all derived classes also changeAny changes in the derived class do not change the base class

Page 50: OOP Inheritance
Page 51: OOP Inheritance
Page 52: OOP Inheritance
Page 53: OOP Inheritance

What really happens?

The object is created using new

Page 54: OOP Inheritance

What really happens?

The object is created using new

The system allocates enough

memory to hold all its instance

variables.

Page 55: OOP Inheritance

What really happens?

The object is created using new

The system allocates enough

memory to hold all its instance

variables.This includes any inherited instance

variables

Page 56: OOP Inheritance

Inheritance and Encapsulation

• Three levels of access control

Page 57: OOP Inheritance

Inheritance and Encapsulation

• Three levels of access control• Public: members (data and methods)

can be used by the class and everybody else (other classes, functions, etc.)

• Protected: members can be accessed by the class (and its friends) and its derived classes

• Private: members can be accessed only by the class (and its friends)

Page 58: OOP Inheritance

Inheritance and Encapsulation

• Three levels of access control• Public: members (data and methods)

can be used by the class and everybody else (other classes, functions, etc.)

• Protected: members can be accessed by the class (and its friends) and its derived classes

• Private: members can be accessed only by the class (and its friends)

• Remark: without inheritance private and protected are the same

Page 59: OOP Inheritance

Inheritance and Encapsulation

• private member– Is accessible only via the base class

• public member– Is accessible everywhere (base class, derived class,

other classes)

• protected member– Is accessible by the base class and derived classes

Page 60: OOP Inheritance
Page 61: OOP Inheritance

What Are Constructors?

Page 62: OOP Inheritance

What Are Constructors?

• Classes use constructors to initialize instance variables

Page 63: OOP Inheritance

What Are Constructors?

• Classes use constructors to initialize instance variables

• When a subclass object is created, its constructor is called.

Page 64: OOP Inheritance

What Are Constructors?

• Classes use constructors to initialize instance variables

• When a subclass object is created, its constructor is called

• It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors

Page 65: OOP Inheritance

What Are Constructors?

• Classes use constructors to initialize instance variables

• When a subclass object is created, its constructor is called

• It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors

• This ensures instance variables defined in the superclass are properlyinitialized

Page 66: OOP Inheritance

Calling the Super Class Constructor

Superclass constructors can be called using the "super" keyword

Page 67: OOP Inheritance

Calling the Super Class Constructor

Superclass constructors can be called using the "super" keywordIt must be the first line of code in the sub class constructor

Page 68: OOP Inheritance

Calling the Super Class Constructor

Superclass constructors can be called using the "super" keywordIt must be the first line of code in the sub class constructorIf a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass.

Page 69: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

Page 70: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

What is this?

Page 71: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

Constructor

Page 72: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

What is this?

Page 73: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

extends indicates inheritance

Page 74: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

What is this?

Page 75: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

What is this?

Page 76: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

Subclass Constructor

Page 77: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

Subclass Constructor

additional parameter

Page 78: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

What is this?

Page 79: OOP Inheritance

Constructors - Example

public class BankAccount

{

private String ownersName;

private int accountNumber;

private float balance;

public BankAccount(int anAccountNumber, String aName)

{

accountNumber = anAccountNumber;

ownersName = aName;

}

[...]

}

public class OverdraftAccount extends BankAccount

{

private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)

{

super(anAccountNumber, aName);

overdraftLimit = aLimit;

}

}

Call to super class constructor

Page 80: OOP Inheritance

A portion of a Shape class hierarchy.

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

Page 81: OOP Inheritance

The Shape and Circle ClassesSide by Side

Page 82: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 83: OOP Inheritance

Abstract Classes & Interfaces

Definitions

• abstract methods = Methods that are declared, with no implementation

• abstract class = A class with abstract methods, not meant to be instantiated

• interface = A named collection of method definitions (without implementations)

Examples

• Food is an abstract class. Can you make an instance of food? No, of course not. But you can make an instance of an apple or a steak or a peanut butter cup, which are types of food. Food is the abstract concept; it shouldn’t exist.

• Skills are interfaces. Can you make an instance of a student, an athlete or a chef? No, but you can make an instance of a person, and have that person take on all these skills. Deep down, it’s still a person, but this person can also do other things, like study, sprint and cook.

Page 84: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 85: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 86: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 87: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 88: OOP Inheritance

Abstract and Interface Classes Revisited

Abstract Classes and Interfaces

cannot be instantiated

may contain a mix of methods declared with or without an implementation.

With abstract classes,

you can declare fields that are not static and final

you can define public, protected, and private concrete methods.

With interfaces,

all fields are automatically public, static, and final

all methods that you declare or define (as default methods) are public.

Page 89: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 90: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 91: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 92: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 93: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 94: OOP Inheritance

Method Overriding

• Method Overriding is when the subclass modifies the implementation of a method defined in the superclass.

• It can only be done if the superclass method is public.

• It cannot be done if the superclass method is static.

• Runtime polymorphism is essentially referred as method overriding and can only be implemented through inheritance

Page 95: OOP Inheritance

Overriding vs. Overloading

public class Test {

public static void main(String[] args) {

A a = new A();

a.p(10);

a.p(10.0);

}

}

class B {

public void p(double i) {

System.out.println(i * 2);

}

}

class A extends B {

// This method overrides the method in B

public void p(double i) {

System.out.println(i);

}

}

public class Test {

public static void main(String[] args) {

A a = new A();

a.p(10);

a.p(10.0);

}

}

class B {

public void p(double i) {

System.out.println(i * 2);

}

}

class A extends B {

// This method overloads the method in B

public void p(int i) {

System.out.println(i);

}

}

Page 96: OOP Inheritance

The Shape and Circle

ClassesSide by

Side

Page 97: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 98: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 99: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 100: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 101: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 102: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 103: OOP Inheritance

The Shape and Rect

ClassSide by Side

Page 104: OOP Inheritance

TestShapes Class

Page 105: OOP Inheritance

TestShapes Class

Which is from Shape and which is from Circ or Rect?

Page 106: OOP Inheritance

Output

CIRCLE

(X,Y) Position: (4,4)

Radius: 5

Area: 78.53981633974483

RECTANGLE

(X,Y) Position: (4,4)

Width & Height: 5 10

Area: 50.0

Page 107: OOP Inheritance

Output

CIRCLE

(X,Y) Position: (4,4)

Radius: 5

Area: 78.53981633974483

RECTANGLE

(X,Y) Position: (4,4)

Width & Height: 5 10

Area: 50.0

Which is from Shape and which is from Circ or Rect?

Page 108: OOP Inheritance

How to do Multiple Inheritance in Java?

• Java does not support multiple inheritance.

Page 109: OOP Inheritance

How to do Multiple Inheritance in Java?

• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces.

Page 110: OOP Inheritance

How to do Multiple Inheritance in Java?

• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces. • You can extend only one class (inheritance) but can implement any number

of interfaces.

Page 111: OOP Inheritance

How to do Multiple Inheritance in Java?

• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces. • You can extend only one class (inheritance) but can implement any number

of interfaces.

Example: under the assumption that Car and Automobile are interfaces.

Page 112: OOP Inheritance

How to do Multiple Inheritance in Java?

• Java does not support multiple inheritance.• You can achieve partial multiple inheritance with the help of interfaces. • You can extend only one class (inheritance) but can implement any number

of interfaces.

Example: under the assumption that Car and Automobile are interfaces.

public class FerrariF12011 extends Ferrari implements Car, Automobile

{

}

Page 113: OOP Inheritance

Summary

• We explored the Object Oriented programming concept - Inheritance

• We identified class hierarchies, single and multiple inheritance

• We defined the syntax of inheritance in Java

• We analyzed super and sub classes

• We examined the effect of inheritance on encapsulation and constructors