CS111: PROGRAMMING LANGUAGE II

Similar documents
CS111: PROGRAMMING LANGUAGE II

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

BBM 102 Introduction to Programming II Spring Inheritance

Inheritance Introduction. 9.1 Introduction 361

Cpt S 122 Data Structures. Inheritance

CS111: PROGRAMMING LANGUAGE II

Inheritance (Deitel chapter 9)

Chapter 7. Inheritance

CS111: PROGRAMMING LANGUAGE II

Object Oriented Design

Inheritance Motivation

A A B U n i v e r s i t y

Java How to Program, 8/e

Computer Programming Inheritance 10 th Lecture

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

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

Programming in C# Inheritance and Polymorphism

What is Inheritance?

Chapter 9 - Object-Oriented Programming: Inheritance

Chapter 5 Object-Oriented Programming

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

Inheritance and Polymorphism

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Object-Oriented Programming: Polymorphism

CS-202 Introduction to Object Oriented Programming

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lecture 5: Inheritance

Programming II (CS300)

CST141--Inheritance 2

PROGRAMMING LANGUAGE 2

ITI Introduction to Computing II

9/10/2018 Programming Data Structures Inheritance

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

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

Lecture 18 CSE11 Fall 2013 Inheritance

ITI Introduction to Computing II

Object- Oriented Programming: Inheritance

Java Object Oriented Design. CSC207 Fall 2014

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Inheritance -- Introduction

Java Programming Lecture 7

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Inheritance (IS A Relationship)

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

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

Programming II (CS300)

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

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

Inheritance and Interfaces

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Comp 249 Programming Methodology

Chapter 6 Introduction to Defining Classes

Data Structures (list, dictionary, tuples, sets, strings)

ECE 122. Engineering Problem Solving with Java

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Inheritance, Polymorphism, and Interfaces

What are the characteristics of Object Oriented programming language?

CS 251 Intermediate Programming Inheritance

9/17/2018 Programming Data Structures. Encapsulation and Inheritance

COMP 110/L Lecture 19. Kyle Dewey

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

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

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

8. Polymorphism and Inheritance

CMSC 132: Object-Oriented Programming II

CS1150 Principles of Computer Science Objects and Classes

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

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

Instance Members and Static Members

Introduction to Object-Oriented Programming

INHERITANCE. Spring 2019

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Chapter 10 Classes Continued. Fundamentals of Java

CMSC 132: Object-Oriented Programming II. Inheritance

Object-Oriented Concepts

C++ Important Questions with Answers

Computer Science 210: Data Structures

More on Objects in JAVA TM

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

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

CLASSES AND OBJECTS IN JAVA

Inheritance (continued) Inheritance

Advanced Programming Using Visual Basic 2008

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

OBJECT ORİENTATİON ENCAPSULATİON

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

Transcription:

CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 4&5: Inheritance

Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword @override keyword Creating subclasses Protected members What is protected? Data Hiding issues Inheritance Hierarchy Case study : Campus Community Constructors call chain

OOP

OOP - Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own.

OOP - Inheritance Inheritance represents the is a relationship between data types (e.g. student/person) Existing class is the superclass (more general) New class is the subclass (more specific) Java supports only single inheritance each class is derived from exactly one direct superclass. Possible to Create hierarchy of classes

Superclasses and Subclasses Superclasses more general subclasses more specific. every subclass object is an object of its superclass

The Object Class The Java class hierarchy begins with class Object (in package java.lang) Every class in Java directly or indirectly extends (or inherits from ) Object. All classes in Java inherit directly or indirectly from Object, so its 11 methods are inherited by all other classes.

The Object Class

The Object Class

The Object Class learn more about Object s methods at: java.sun.com/javase-6/docs/api/java/lang/object.html Or java.sun.com/docs/books/tutorial/java/iandi/ objectclass.html

Apply on Student class Does your class extends Object? If you don t explicitly specify which class a new class extends, the class extends Object implicitly. What methods did you redefine (override)? finalize() tostring() equals() To override a superclass method, a subclass must declare a method with the same signature as the superclass method or a compilation error occurs @Override annotation

Programming Pitfalls!!

Superclasses and Subclasses Inheritance issue A subclass can inherit methods that it does not need or should not have. Even when a superclass method is appropriate for a subclass, that subclass often needs a customized version of the method. The subclass can override (redefine) the superclass method with an appropriate implementation. An overrided subclass method can access the superclass method using the keyword super and a dot (.) separator.

Example: CommissionEmployee Class 14 Inheritance hierarchy containing types of employees in a company s payroll application Commission employees are paid a percentage of their sales

Class CommissionEmployee 15 Extends object not required; this will be done implicitly Constructors are not inherited

Class CommissionEmployee 16 Set/get methods

Class CommissionEmployee 17 Set/get methods

Class CommissionEmployee 18 Set/get methods

Class CommissionEmployee 19 @Override ensures overridded method has the same signature as the base class

Test 20 New employee object

Test 21 Implicit call to the method tostring

Creating a new class!! 22 Base-salaried commission employees receive a base salary plus a percentage of their sales. Class BasePlusCommissionEmployee: Data: first name, last name, social security number, gross sales amount, commission rate and base salary. (( All but the base salary are in common with class CommissionEmployee)). services: a constructor, and methods earnings, tostring and get and set for each instance variable ((Most are in common with class CommissionEmployee ))

Creating a new class 23 Copy & paste Inheritance copy CommissionEmployee code, pasted it into BasePlusCommissionEm ployee modify the new class to include a base salary and methods that manipulate the base salary. error prone time consuming. Too many copies of the same code bad maintenance Extend an existing class and add only the needed data/functionality Reusability Don t reinvent the wheel!!

Goal : code reuse 24 Inheritance can save time during program development: rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. by basing new classes on existing proven and debugged high-quality software. Increases the likelihood that a system will be implemented and maintained effectively.

BasePlusCommissionEmployee Class 25 New class defined based on an existing one Subclass constructor must call the superclass one to handle initialization

BasePlus-CommissionEmployee Class 26 public members are accessible from anywhere private members are accessible only within the class itself.

Errors!! 27 A superclass s private members are hidden in its subclasses Compilation errors occur when the subclass attempts to access the superclass s private instance variables.

Protected Members 28 To enable a subclass to directly access superclass instance variables, we can declare those members as protected in the superclass. an intermediate level of access between public and private. can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same package (package access). All public and protected superclass members retain their original access modifier when they become members of the subclass.

29 Programming Pitfalls To enable a subclass to directly access superclass instance variables, we can declare those members as protected in the superclass

Constructors in Subclasses Constructors are not inherited. The first task of a subclass constructor is to call its direct superclass s constructor explicitly or implicitly Ensures that the instance variables inherited from the superclass are initialized properly. If the code does not include an explicit call to the superclass constructor, Java implicitly calls the superclass s default or no-argument constructor. A class s default constructor calls the superclass s default or no-argument constructor.

Notes on Using protected Instance Variables Using protected instance variables creates several potential problems. The subclass object can set an inherited variable s value directly without using a set method. A subclass object can assign an invalid value to the variable, possibly leaving the object in an inconsistent state. Subclasses should depend only on the superclass services and not on the superclass data implementation.

Notes on Using protected Instance Variables With protected instance variables in the superclass, we may need to modify all the subclasses of the superclass if the superclass implementation changes. Such software is said to be fragile or brittle, because a small change in the superclass can break subclass implementation. You should be able to change the superclass implementation while still providing the same services to the subclasses. If the superclass services change, we must reimplement our subclasses. A class s protected members are visible to all classes in the same package as the class containing the protected members this is not always desirable.

Programming Pitfalls!!

Data Hiding What if data is private?? Encapsu lation better design

New class Use set/get, either within class or in subclasses Information hiding

Reusability!!

42 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 5: Inheritance Hierarchy

Inheritance Hierarchy A superclass exists in a hierarchical relationship with its subclasses. Each arrow in the Inheritance hierarchy represents an is-a relationship. The direct superclass is the superclass from which the subclass explicitly inherits. An indirect superclass is any class above the direct superclass in the class hierarchy.

Inheritance Hierarchy Starting from the bottom, you can follow the arrows and apply the is-a relationship up to the topmost superclass. A Triangle is a TwoDimensionalShape and is a Shape A Sphere is a ThreeDimensionalShape and is a Shape.

Constructors call chain Instantiating a subclass object begins a chain of constructor calls The subclass constructor, before performing its own tasks, invokes its direct superclass s constructor If the superclass is derived from another class, the superclass constructor invokes the constructor of the next class up the hierarchy, and so on. The last constructor called in the chain is always class Object s constructor. Original subclass constructor s body finishes executing last. Each superclass s constructor manipulates the superclass instance variables that the subclass object inherits.

Case Study: Campus Community CommunityMember is the direct superclass of Employee, Student and Alumnus and is an indirect superclass of all the other classes in the diagram.

Class Design 47 CommunityMember First Name Last Name SSN BirthDate Computer Science Department

Class Design 48 Employee Student Title HireDate Salary UnvID Faculty Level Computer Science Department

Class Design 49 Degree(BSc, MSc, PhD) Specialty Faculty Staff Full/partTime Computer Science Department

Constructors in Subclasses Constructors are not inherited. The first task of a subclass constructor is to call its direct superclass s constructor explicitly or implicitly Ensures that the instance variables inherited from the superclass are initialized properly. If the code does not include an explicit call to the superclass constructor, Java implicitly calls the superclass s default or no-argument constructor. A class s default constructor calls the superclass s default or no-argument constructor.

51 That s all,,, Text Book ref. Chapter 9 Computer Science Department