Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Similar documents
OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Transitivity

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Chapter 10 Classes Continued. Fundamentals of Java

More Relationships Between Classes

Inheritance and Polymorphism

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Exercise: Singleton 1

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

First IS-A Relationship: Inheritance

Object Orientated Programming Details COMP360

Arrays Classes & Methods, Inheritance

Chapter 5. Inheritance

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Outsource: )

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Lecture 13 Summary. Assignment 3 Discussion. Polymorphism 3/4/2009. Assignment 3 Polymorphism Interfaces

Chapter 14 Abstract Classes and Interfaces

AP CS Unit 6: Inheritance Notes

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance and Substitution (Budd chapter 8, 10)

Programming II (CS300)

Programming II (CS300)

Instance Members and Static Members

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Polymorphism. Agenda

Inheritance and Polymorphism

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Inheritance and Polymorphism

Inheritance (Part 5) Odds and ends

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance (an intuitive description)

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Lecture 2: Java & Javadoc

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

What is Inheritance?

Object-Oriented Design

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

QUESTIONS FOR AVERAGE BLOOMERS

Computer Science 210: Data Structures

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

Chapter 3: Inheritance and Polymorphism

Tutorial 2 Today we will have a quick introduction to ArgoUML and how to use it, as well as look at some examples of class diagrams.

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Example: Count of Points

Inheritance (Part 2) Notes Chapter 6

Object-Oriented Concepts

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

COMP 110/L Lecture 19. Kyle Dewey

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS1150 Principles of Computer Science Objects and Classes

K.Yellaswamy Assistant Professor CMR College of Engineering & Technology

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

CS 251 Intermediate Programming Inheritance

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CSC207 Week 3. Larry Zhang

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

The major elements of the object-oriented model

INHERITANCE Mrs. K.M. Sanghavi

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Interfaces

INHERITANCE AND EXTENDING CLASSES

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Lecture 02, Fall 2018 Friday September 7

CSC 1214: Object-Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

What are the characteristics of Object Oriented programming language?

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CS Programming I: Inheritance

Example: Count of Points

Chapter 5 Object-Oriented Programming

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CS Week 15 Page 1

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Rules and syntax for inheritance. The boring stuff

Computer Science II (20073) Week 1: Review and Inheritance

More On inheritance. What you can do in subclass regarding methods:

Java Fundamentals (II)

Inheritance -- Introduction

Inheritance and Interfaces

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Transcription:

Lecture 12 Summary Inheritance Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods Multiplicity 1

By the end of this lecture, you will be able to incorporate inheritance and multiplicity into your class models. You will also be able to use inheritance and override methods in Java. 2

Process So far... Identify objects and create an object model Observe commonalities in object model objects with the same attributes/behaviour Classify common objects into a class model remove repetition (number of objects/relations) 3

Additional Step Identify objects and create an object model Observe commonalities in object model objects with the same attributes/behaviour Classify common objects into a class model remove repetition (number of objects/relations) Find commonalities in class model and abstract them using inheritance. this process is called generalization 4

In procedural languages, what is the benefit of dividing your code up into multiple procedures? In OO languages, what is the benefit of classifying objects in your object model? 5

The process of abstraction helps to reduce the complexity of the problem space. Generalization is another form of abstraction. 6

Draw the class model... Mountain Bike Frame Rim Wheel Tire Bike Shop Shocks Handle Bars Spokes Commuter Bike Rack Frame Handle Bars Wheel Rim Tire Spokes 7

What are the commonalities in the object model? What are the commonalities in the class model? 8

Inheritance A new kind of relationship between classes: is-a or is-a-kind-of Used to describe a group of classes in an abstract way 9

Example Eye Animal Ear... Dog Elephant Human Fur Trunk 10

Terminology Superclass Animal Dog Elephant Human Subclasses 11

Terminology Parent Class Animal Dog Elephant Human Child Classes 12

Exercise: generalize the bike shop class model. 13

Java Syntax public class <subclass> extends <superclass> {... 14

Example Animal.java: public class Animal { private Eye lefteye; private Eye righteye; Dog.java: public class Dog extends Animal { private Fur fur; private Ear leftear; private Ear rightear; Elephant.java: public class Elephant extends Animal { private Trunk trunk; Human.java: public class Human extends Animal { 15

Which code makes sense? 1. public class MainProgram { 2. public static void main(string[] args) { 3. Animal a = new Dog(); 4. Elephant e = new Animal(); 5. 6. LinkedList<Animal> animals = 7. new LinkedList<Animal>(); 8. animals.add( new Human() ); 9. animals.add( new Elephant() ); 10. 11. LinkedList<Dog> dogs = 12. new LinkedList<Dog>(); 13. dog.add( new Dog() ); 14. dog.add( new Human() ); 15. 16. 16

What if we want a method called speak that plays the right sound for each animal? Where does this method belong? 17

Consider the following code... public class MainProgram { public static void main(string[] args) { LinkedList<Animal> animals = new LinkedList<Animal>();... // fill up the list for (Animal a : animals) { a.speak(); What should happen here? 18

Overriding Methods A subclass can override a method in the superclass Automatically happens by using the same method signature same name same parameters same return type 19

Example public class Animal {... public class Dog extends Animal {... public void speak() {... public void speak() { playaudioclip("bark.wav"); 20

Abstract Classes & Methods An abstract class is a class that cannot be instantiated No instances can be created An abstract method is a method that must be overridden by any subclass 21

Example public abstract class Animal {... public abstract void speak(); 22

Reconsider the following code... public class MainProgram { public static void main(string[] args) { LinkedList<Animal> animals = new LinkedList<Animal>();... // fill up the list for (Animal a : animals) { a.speak(); 23

Can you think of a situation where you would want to prevent a method from being overridden? 24

final You can prevent a method from being overridden by adding the keyword final. You can prevent a class from being inherited from by adding the keyword final. 25

Example public class Security { public final boolean matchpassword(string password) {... 26

Does the following code make sense? public abstract final ParentClass { 27

Root Classes There is a class called Object in Java Every class is an Object. If you do not specify a superclass through the extends keyword, Java automatically inherits from Object Object is the root class for Java Not all languages have a root class (e.g., C++) 28

Single Inheritance Java only allows single inheritance: A class can only have one superclass This superclass may itself inheret from another class, and so on, until Object is reached Object is the only class with no superclass C++ allows for multiple inheritance A class in C++ can have many superclasses 29

Inheritance Summary Commonalities between classes in the class model can be abstracted using inheritance. Inheritance introduces the is-a relationship to our class models. In Java, a class can inherit from a superclass using the extends keyword. An instance of a subclass can be substituted for a reference to a superclass 30

Inheritance Summary Methods in a superclass can be overridden in a subclass An abstract class cannot be instantiated (only its non-abstract subclasses can). You can prevent a class from being subclassed or a method from being overridden with the final keyword. Java uses single inheritance and has a root class called Object. 31

Multiplicity The has-a relationship can be further decomposed: has one has many belongs to one belongs to many 32

Multiplicity Driver 1 1 Car Tree 1 * Leaf Purchase * 1 Customer Document * * Word 33

Exercise: add indications in the bike shop class model for multiplicity. 34

Lecture 12 Summary Inheritance Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods Multiplicity 35

Next Class Polymorphism (theory) Interfaces (a.k.a. pure abstract classes) 36