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

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

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

BBM 102 Introduction to Programming II Spring Inheritance

Object Oriented Design

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 5 Object-Oriented Programming

Java How to Program, 8/e

CS111: PROGRAMMING LANGUAGE II

Cpt S 122 Data Structures. Inheritance

Inheritance and Polymorphism

Inheritance Introduction. 9.1 Introduction 361

PROGRAMMING LANGUAGE 2

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance -- Introduction

Java Object Oriented Design. CSC207 Fall 2014

Object-Oriented Programming: Polymorphism

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

Java Programming Lecture 7

Chapter 7. Inheritance

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance (IS A Relationship)

Programming II (CS300)

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

What is Inheritance?

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

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

Polymorphism and Inheritance

Programming II (CS300)

WEEK 13 EXAMPLES: POLYMORPHISM

9/10/2018 Programming Data Structures Inheritance

Inheritance and Polymorphism

What are the characteristics of Object Oriented programming language?

final Methods and Classes

CLASSES AND OBJECTS IN JAVA

Inheritance, Polymorphism, and Interfaces

Chapter 14 Abstract Classes and Interfaces

C++ Polymorphism. Systems Programming

Object-Oriented Concepts

CMSC 132: Object-Oriented Programming II

ECE 122. Engineering Problem Solving with Java

Object-Oriented Programming

Practice for Chapter 11

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

CS-202 Introduction to Object Oriented Programming

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

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

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

JAVA MOCK TEST JAVA MOCK TEST II

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Comp 249 Programming Methodology

Arrays Classes & Methods, Inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Object-Oriented Programming (OOP) Fundamental Principles of OOP

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Chapter 10 Classes Continued. Fundamentals of Java

Computer Programming Inheritance 10 th Lecture

Object-Oriented Programming: Polymorphism Pearson Education, Inc. All rights reserved.

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

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

8. Polymorphism and Inheritance

Inheritance (Part 5) Odds and ends

Lecture 5: Inheritance

INHERITANCE AND EXTENDING CLASSES

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

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

C++ Important Questions with Answers

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

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

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

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

[ L5P1] Object-Oriented Programming: Advanced Concepts

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

Lecture 4: Extending Classes. Concept

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

CS 251 Intermediate Programming Inheritance

Microsoft Visual Basic 2005: Reloaded

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

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

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

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

More on Objects in JAVA TM

CLASS DESIGN. Objectives MODULE 4

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

Exercise: Singleton 1

Advanced Programming Using Visual Basic 2008

ITI Introduction to Computing II

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

Programming Exercise 14: Inheritance and Polymorphism

CS260 Intro to Java & Android 03.Java Language Basics

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CIS 110: Introduction to computer programming

Inheritance. Transitivity

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

INHERITANCE. Spring 2019

Transcription:

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses Polymorphism 1

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

OOP - Inheritance 5 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) C# supports only single inheritance each class is derived from exactly one direct superclass. Possible to Create hierarchy of classes Superclasses and Subclasses 6 Superclasse s more general subclasses more specific. every subclass object is an object of its superclass 3

Protected Members 7 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. All public and protected superclass members retain their original access modifier when they become members of the subclass. Not recommended to enforce information hiding Example: CommissionEmployee Class 8 Inheritance hierarchy containing types of employees in a company s payroll application Commission employees are paid a percentage of their sales 4

The code 9 A colon (:) indicates inheritance Every C# class directly or indirectly inherits object s methods. 10 5

11 virtual!! 12 A virtual method is ready to be overridden in the subclasses 6

Override!! 13 To override a base-class method, a derived class must declare a method with keyword override. the same signature Creating a new class!! 14 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 )) 7

Creating a new class 15 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!! The new class 16 Constructors are not inherited : The derived-class constructor, before performing its own tasks, invokes its direct base class s constructor 8

17 18 9

19 Tip!! Test 20 10

21 Polymorphism 22 a superclass reference at a subclass object. (crossover) Allowed because each subclass object is an object of its superclass. The type of the referenced object, not the type of the variable, determines which method is called. Invoking a method on a subclass object via a superclass reference invokes the subclass functionality 11

Example 23 Same base references, different objects Example 24 When the compiler encounters a virtual method call made through a variable, the compiler checks the variable s class type to determines if the method can be called. 12

25 26 Chapter 11 Chapter 12 : 12.3 13

27 Case Study 28 14