Polymorphism. Chapter 4. CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science. Dr. S.

Size: px
Start display at page:

Download "Polymorphism. Chapter 4. CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science. Dr. S."

Transcription

1 Chapter 4 Polymorphm CSC 113 King Saud University College Computer and Information Sciences Department Computer Science

2 Objectives After you have read and studied th chapter, you should be able to Write programs that are easily extensible and modifiable by applying polymorphm in program design. Define reusable es based on inheritance and abstract es and abstract s. Differentiate abstract es and Java interfaces. Define s, using protected modifier. Parse strings, using a Tokenizer.

3 Introduction to Polymorphm There are three main programming mechanms that constitute -oriented programming OOP Encapsulation Inheritance Polymorphm Polymorphm ability to associate many meanings to one It does th through a special mechanm known as late binding or dynamic binding A polymorphic one that has same for different es same family, but has different implementations, or behavior, for various es.

4 Introduction to Polymorphm Polymorphm When a program invokes a through a super variable, correct sub version called, based on type reference stored in super variable The same and signature can cause different actions to occur, deping on type on which invoked Facilitates adding es to a system with minimal modifications to system s code

5 Example Demonstrating Polymorphic Behavior A polymorphic ex dplay A that has multiple meanings Created when a sub overrides a super Base dplay Doubler Tripler dplay dplay Squarer dplay

6 Example Demonstrating Polymorphic Behavior Base protected int i 100;... dplay System.out.println i ; Doubler exts Base... dplay System.out.println i*2 ; Tripler exts Base... dplay System.out.printlni*3 ; Squarer exts Tripler... dplay System.out.println i*i ;

7 Example Demonstrating Polymorphic Behavior Case Static binding Some main program output Base B Base; B. dplay; 100 Doubler D Doubler; D. dplay; 200 Tripler T Tripler; T. dplay; 300 Squarer S Squarer; S. dplay; Static binding occurs when a defined with same but with different headers and implementations. The actual code for attached, or bound, at compile time. Static binding used to support overloaded s in Java.

8 Example Demonstrating Polymorphic Behavior Case Dynamic binding A super reference can be aimed at a sub Th possible because a sub a super as well When invoking a from that reference, type actual referenced, not type reference, determines which called Some main program Base B Base; B. dplay; Base D; D Doubler; D. dplay; output Late binding or dynamic binding The appropriate version a polymorphic decided at execution time Base T; T Tripler; T. dplay; 300 Base S; S Squarer; S. dplay; 10000

9 Example Inheritance Hierarchy Class Student Polymorphm case #NUM_OF_TESTS int 3 # string #test [] int Student Student Studentin studentname string setscorein s1 int, in s2 int, in s3 int setnamein Name string gettestscore int getcoursegrade string settestscorein testnumber int, in testname string getname string computecoursegrade GraduateStudent UnderGraduateStudent computecoursegrade computecoursegrade

10 Example Inheritance Hierarchy Class Student Polymorphm case Creating roster Array We mentioned in array definition that an array must contain elements same data type. For example, we can t store integers and real numbers in same array. To follow th rule, it seems necessary for us to declare two separate arrays, one for graduate and anor for undergraduate students. Th rule, however, does not apply when array elements are s using polymorphm. We only need to declare a single array. We can create roster array combining s from Student, UndergraduateStudent, and GraduateStudent es. Student roster Student[40];... roster[0] GraduateStudent; roster[1] UndergraduateStudent; roster[2] UndergraduateStudent;...

11 State roster Array The roster array with elements referring to instances GraduateStudent or UndergraduateStudent es.

12 Sample Polymorphic Message To compute course grade using roster array, we execute for int i 0; i < numberofstudents; i roster[i].computecoursegrade; If roster[i] refers to a GraduateStudent, n computecoursegrade GraduateStudent executed. If roster[i] refers to an UndergraduateStudent, n computecoursegrade UndergraduateStudent executed.

13 The instance Operator The instance operator can help us learn an. The following code counts number undergraduate students. int undergradcount 0; for int i 0; i < numberofstudents; i if roster[i] instance UndergraduateStudent undergradcount;

14 Case Study Implementation Student in Java Student Student GraduateStudent GraduateStudent exts exts Student Student protected protected final final static static int int NUM_OF_TESTS NUM_OF_TESTS 3; 3; protected protected ; ; /** /** protected protected int[] int[] test; test; * * students. students. Pass Pass if if total total > > 80; 80; orwe, orwe, No No Pass. Pass. protected protected coursegrade; coursegrade; */ */ Student Student th th No No Name; Name; computecoursegrade computecoursegrade Student Student studentname studentname int int total total 0; 0; studentname; studentname; for for int int i i 0; 0; i i < < NUM_OF_TESTS; NUM_OF_TESTS; i i test test int[num_of_tests]; int[num_of_tests]; total total test[i]; test[i]; coursegrade coursegrade ****; ****; if if total total > > coursegrade coursegrade Pass; Pass; setscoreint setscoreint s1, s1, int int s2, s2, int int s3 s3 else else coursegrade coursegrade No No Pass; Pass; test[0] test[0] s1; s1; test[1] test[1] s2; s2; test[2] test[2] s3; s3; computecoursegrade computecoursegrade coursegrade; coursegrade; getcoursegrade getcoursegrade UnderGraduateStudent UnderGraduateStudent exts exts Student Student coursegrade; coursegrade; getname getname ; ; computecoursegrade computecoursegrade int int int gettestscoreint gettestscoreint testnumber testnumber int total total 0; 0; for test[testnumber-1]; test[testnumber-1]; for int int i i 0; 0; i i < < NUM_OF_TESTS; NUM_OF_TESTS; i i total setname setname Name Name total test[i]; test[i]; if Name; Name; if total total > > coursegrade settestscoreint settestscoreint testnumber, testnumber, int int testscore coursegrade Pass; Pass; testscore test[testnumber-1]testscore; test[testnumber-1]testscore; else else coursegrade coursegrade No No Pass; Pass;

15 Implementation StudentTest in Java Case Study StudentTest StudentTest static static main[] main[] args args Student Student roster[] roster[] Student[2]; Student[2]; roster[0] roster[0] GraduateStudent; GraduateStudent; roster[1] roster[1] UnderGraduateStudent; UnderGraduateStudent; roster[0].setscore roster[0].setscore 20, 20, 30, 30, 50; 50; roster[1].setscore roster[1].setscore 10, 10, 17, 17, 13; 13; for for int int i0; i0; i<roster.length; i<roster.length; i i System.out.printlnThe System.out.printlnThe roster[i].getclass.getname; roster[i].getclass.getname; roster[i].computecoursegrade; roster[i].computecoursegrade; System.out.println System.out.println Pass Pass or or Not Not roster[i].getcoursegrade; roster[i].getcoursegrade; execution The GraduateStudent Pass or Not Pass The UnderGraduateStudent Pass or Not No Pass If roster[i] refers to a GraduateStudent, n computecoursegrade GraduateStudent executed. If roster[i] refers to a UnderGraduateStudent, n computecoursegrade UnderGraduateStudent executed. We call message computecoursegrade polymorphic

16 Implementation StudentTest2 in Java Case Study Question Count number under graduate students StudentTest2 StudentTest2 static static main[] main[] args args Student Student roster[] roster[] Student[2]; Student[2]; roster[0] roster[0] GraduateStudent; GraduateStudent; roster[1] roster[1] UnderGraduateStudent; UnderGraduateStudent; roster[0].setscore roster[0].setscore 20, 20, 30, 30, 50; 50; roster[1].setscore roster[1].setscore 10, 10, 17, 17, 13; 13; int int nb0; nb0; count count number number Under Under Graduate Graduate Students Students for for int int i0; i0; i<roster.length; i<roster.length; i i if if roster[i] roster[i] instance instance UnderGraduateStudent UnderGraduateStudent nb; nb; System.out.println The System.out.println The number number Under Under Graduate Graduate Students Students nb; nb; execution The number Under Graduate Students 1 Rule To Determine an, we use instance operator.

17 Example Inheritance Hierarchy Class dplay dplays area and perimeter Of a shape area perimeter dplay Rectangle -width -length Rectanglein wid, in leng getwidth getlength area perimeter Square -side Squarein sid getside area perimeter

18 Test inheritance Super-Class Test Test static static main[] main[] args args shp shp ; ; shp shp an an from from Rectangle Rectangle rect rect Rectangle4.0, Rectangle4.0, 5.0; 5.0; rect rect an an from from Rectangle Rectangle Square Square sqr sqr Square6.0; Square6.0; sqr sqr an an from from Square Square shp.dplay; shp.dplay; uses uses dplay dplay from from rect.dplay; rect.dplay; rect rect inherits inherits dplay dplay from from Super Super sqr.dplay; sqr.dplay; sqr sqr inherits inherits dplay dplay from from Super Super execution execution The The The The area area The The perimeter perimeter The The Rectangle Rectangle The The area area 2 2 The The perimeter perimeter The The Square Square The The area area The perimeter 24.0

19 Case Study 3 Implementation inheritance in Java area area ; ; perimeter perimeter ;; ;; dplay dplay System.out.printlnThe System.out.printlnThe th.getclass.getname; th.getclass.getname; getclass getclass a a inherits inherits from from super super Object. Object getname getname a a from from.. System.out.printlnThe System.out.printlnThe area area area; area; System.out.printlnThe System.out.printlnThe perimeter perimeter perimeter\n\n; perimeter\n\n; Rectangle Rectangle exts exts width; width; length; length; Rectangle Rectangle length, length, width width th.length th.length length; length; th.width th.width width; width; getlength getlength length; length; getwidth getwidth width; width; area area th.getlength*th.getwidth; th.getlength*th.getwidth; perimeter perimeter 2*th.getLengthth.getWidth; 2*th.getLengthth.getWidth; Square Square exts exts side; side; Square Square side side th.side th.side side; side; getside getside side; side; area area th.getside*th.getside; th.getside*th.getside; perimeter perimeter 4*th.getSide; 4*th.getSide;

20 Introduction to Abstract Classes In following example, we want to add to a dplay that prints area and perimeter a shape. # color string Rectangle -width -length Rectanglein wid, in leng getwidth getlength area perimeter Square -side Squarein sid getside area perimeter

21 Introduction to Abstract Classes Abstract Method The following added to dplay System.out.println th.area; System.out.println th.perimeter;

22 Introduction to Abstract Classes Abstract Method There are several problems with th The area and perimeter s are invoked in dplay There are area and perimeter s in each subes There no area and perimeter s in, nor re any way to define it reasonably without knowing wher shape Rectangle or Square.

23 Abstract Class Introduction to Abstract Classes In order to postpone definition a, Java allows an abstract to be declared An abstract has a heading, but no body The body defined in subes The that contains an abstract called an abstract

24 Introduction to Abstract Classes Abstract Method An abstract like a placeholder for a that will be fully defined in a descent It has a complete heading, to which has been added modifier abstract It cannot be It has no body, and s with a semicolon in place its body abstract area; abstract perimeter;

25 Introduction to Abstract Classes # color string dplay dplays area and perimeter a shape area perimeter dplay Rectangle -width -length Rectanglein wid, in leng getwidth getlength area perimeter Square -side Squarein sid getside area perimeter

26 Introduction to Abstract Classes A that has at least one abstract called an abstract An abstract must have modifier abstract included in its heading abstract protected color;... abstract area; abstract perimeter; dplay System.out.println th.area; System.out.println th.perimeter;...

27 Introduction to Abstract Classes An abstract can have any number abstract and/or fully defined s If a derived an abstract adds to or does not define all abstract s, n it abstract also, and must add abstract to its modifier A that has no abstract s called a concrete

28 Pitfall You Cannot Create Instances an Abstract Class An abstract can only be used to derive more specialized es While it may be useful to dcuss shape in general, in reality a shape must be a rectangle form or a square form An abstract cannot be used to create an abstract However, a sub will include an invocation abstract in form super

29 Dynamic Binding and Abstract Classes Controlling wher a sub can override a super Field modifier final Prevents a from being overridden by a sub Field modifier abstract Requires sub to override Early binding or static binding The appropriate version a decided at compilation time Used by s that are final or static

30 hierarchy UML diagram.

31 Example Inheritance Hierarchy Class -fname string -lname string -ssn string setfirstnamein fna string setlastnamein lname string setsnnin snn string getfirstname string getlastname string getsnn string earnings to Salaried -weeklysalary earnings to string Commssion -grosssales -commsionrate earnings to string Hourly -wage -hours earnings to string BasePlusCommssion -basesalary earnings to......

32 Case Study Implementation in Java abstract abstract firstname; firstname; lastname; lastname; socialsecuritynumber; socialsecuritynumber; three-argument three-argument ssn ssn firstname firstname first; first; lastname lastname last; last; socialsecuritynumber socialsecuritynumber ssn; ssn; three-argument three-argument set set first first setfirstname setfirstname first first firstname firstname first; first; setfirstname setfirstname first first getfirstname getfirstname firstname; firstname; getfirstname getfirstname set set last last setlastname setlastname last last lastname lastname last; last; Dr. S. HAMMAMI setlastname setlastname last last getlastname getlastname lastname; lastname; getlastname getlastname set set social social security security number number setsocialsecuritynumber setsocialsecuritynumber ssn ssn socialsecuritynumber socialsecuritynumber ssn; ssn; should should validate validate setsocialsecuritynumber setsocialsecuritynumber social social security security number number getsocialsecuritynumber getsocialsecuritynumber socialsecuritynumber; socialsecuritynumber; getsocialsecuritynumber getsocialsecuritynumber representation representation to to The The getfirstname getfirstname getlastname getlastname \nthe \nthe Social Social Security Security Number Number getsocialsecuritynumber getsocialsecuritynumber ; ; to to abstract abstract overridden overridden by by subes subes abstract abstract earnings; earnings; no no implementation implementation here here abstract abstract

33 Implementation Salaried in Java Case Study Salaried Salaried exts exts calculate calculate earnings; earnings; override override abstract abstract earnings earnings in weeklysalary; in weeklysalary; four-argument four-argument earnings earnings Salaried Salaried getweeklysalary; getweeklysalary; ssn, ssn, earnings earnings super super ssn ssn code code reuse representation representation Salaried Salaried reuse super super ssn ssn ; ; pass pass to to setweeklysalary setweeklysalary ; ; validate validate and and store store th th override override to to super super four-argument four-argument Salaried Salaried to to ***** ***** super.to super.to code code reuse reuse good good example set set example setweeklysalary setweeklysalary super.to super.to \nearnings \nearnings getweeklysalary; getweeklysalary; weeklysalary weeklysalary < <?? ; to to ; th th mean mean that, that, if if <0 <0 n n put put it it 0 0 else else put put it it Salaried Salaried setweeklysalary setweeklysalary getweeklysalary getweeklysalary weeklysalary; weeklysalary; getweeklysalary getweeklysalary

34 Implementation Hourly in Java Case Study Hourly Hourly exts exts wage; wage; wage wage per per hour hour hours; hours; hours hours worked worked for for week week five-argument five-argument Hourly Hourly ssn, ssn, hourlywage, hourlywage, hoursworked hoursworked super super ssn ssn code code reuse reuse super super ssn ssn ; ; setwage setwage hourlywage hourlywage ; ; validate validate and and store store hourly hourly wage wage sethours sethours hoursworked hoursworked ; ; validate validate and and store store hours hours worked worked five-argument five-argument Hourly Hourly setwage setwage hourlywage hourlywage wage wage hourlywage hourlywage < <?? hourlywage; hourlywage; setwage setwage getwage getwage wage; wage; getwage getwage sethours sethours hoursworked hoursworked hours hours hoursworked hoursworked > > && && hoursworked hoursworked < < ?? hoursworked hoursworked ; ; sethours sethours gethours gethours hours; hours; gethours gethours calculate calculate earnings; earnings; override override abstract abstract earnings earnings in in earnings earnings if if gethours gethours < < no no overtime overtime getwage getwage * * gethours; gethours; else else * * getwage getwage gethours gethours * * getwage getwage * * 1.5; 1.5; earnings earnings representation representation Hourly Hourly to to /* /* here here overriding overriding to to super super */ */ /*code /*code reuse reuse using using super. super. */ */ super.to super.to \nhourly \nhourly wage wage getwage getwage \nhours \nhours worked worked gethours gethours \nsalary \nsalary earnings earnings ; ; to to Hourly Hourly

35 Implementation Comsion in Java Case Study Commsion Commsion exts exts grosssales; grosssales; gross gross weekly weekly sales sales commsionrate; commsionrate; commsion commsion percentage percentage five-argument five-argument Commsion Commsion ssn, ssn, sales, sales, rate rate super super ssn ssn ; ; setgrosssales setgrosssales sales sales ; ; setcommsionrate setcommsionrate rate rate ; ; five-argument five-argument Commsion Commsion set set commsion commsion rate rate setcommsionrate setcommsionrate rate rate commsionrate commsionrate rate rate > > && && rate rate < < ?? rate rate ; ; setcommsionrate setcommsionrate commsion commsion rate rate getcommsionrate getcommsionrate commsionrate; commsionrate; getcommsionrate getcommsionrate set set gross gross sales sales amount amount setgrosssales setgrosssales sales sales grosssales grosssales sales sales < <?? sales; sales; setgrosssales setgrosssales gross gross sales sales amount amount getgrosssales getgrosssales grosssales; grosssales; getgrosssales getgrosssales calculate calculate earnings; earnings; override override abstract abstract earnings earnings in in earnings earnings getcommsionrate getcommsionrate * * getgrosssales; getgrosssales; earnings earnings representation representation Commsion Commsion to to super.to super.to \ngross \ngross sales sales getgrosssales getgrosssales \ncommsion \ncommsion rate rate getcommsionrate getcommsionrate \nearnings \nearnings earnings earnings ; ; to to Commsion Commsion

36 Implementation BasePlusComsion in Java Case Study BasePlusCommsion BasePlusCommsion exts exts Commsion Commsion basesalary; basesalary; base base per per week week six-argument six-argument BasePlusCommsion BasePlusCommsion ssn, ssn, sales, sales, rate, rate, super super ssn, ssn, sales, sales, rate rate ; ; setbasesalary setbasesalary ; ; validate validate and and store store base base six-argument six-argument BasePlusCommsion BasePlusCommsion set set base base setbasesalary setbasesalary basesalary basesalary < <?? ; ; nonnegative nonnegative setbasesalary setbasesalary calculate calculate earnings; earnings; override override earnings earnings in in Commsion Commsion earnings earnings getbasesalary getbasesalary super.earnings; super.earnings; code code reuse reuse form form Commsion Commsion earnings earnings representation representation BasePlusCommsion BasePlusCommsion to to \nbase-salaried \nbase-salaried super.to super.to \nbase \nbase getbasesalary getbasesalary \nearnings \nearnings earnings earnings ; ; to to BasePlusCommsion BasePlusCommsion base base getbasesalary getbasesalary basesalary; basesalary; getbasesalary getbasesalary

37 Implementation PayrollSystemTest in Java PayrollSystemTest PayrollSystemTest static static main main args[] args[] create create sub sub s s Salaried Salaried SA SA Salaried Salaried Ali, Ali, Samer, Samer, , , ; ; Hourly Hourly HE HE Hourly Hourly Ramzi, Ramzi, Ibrahim, Ibrahim, , , 16.75, 16.75, ; ; Commsion Commsion CE CE Commsion Commsion Med, Med, Ahmed, Ahmed, , , 10000, 10000, ; ; BasePlusCommsion BasePlusCommsion BP BP BasePlusCommsion Beji, BasePlusCommsion Beji, Lotfi, Lotfi, , , 5000, 5000,.04,.04, ; ; System.out.println System.out.println s s processed processed individually\n individually\n ; ; /* /* salaried salaried same same as as salaried.to salaried.to */ */ System.out.println System.out.println SA.to SA.to \nearned \nearned SA.earnings\n\n SA.earnings\n\n ; ; System.out.println System.out.println HE HE \n \n earned earned HE.earnings\n\n HE.earnings\n\n ; ; System.out.printlnCE System.out.printlnCE \n \n earned earned CE.earnings\n\n CE.earnings\n\n ; ; System.out.printlnBP System.out.printlnBP \n \n earned earned BP.earnings\n\n BP.earnings\n\n ; ; create create four-element four-element array array employees[] employees[] [ [ 4 4 ]; ]; employees[ employees[ 0 0 ] ] SA; SA; employees[ employees[ 1 1 ] ] HE; HE; employees[ employees[ 2 2 ] ] CE; CE; employees[ employees[ 3 3 ] ] BP; BP; System.out.println System.out.println s s processed processed polymorphically\n polymorphically\n ; ; generically generically process process each each element element in in array array employees employees for for current current employees employees System.out.println System.out.println current current ; ; invokes invokes to to here here polymorphysim polymorphysim call call to to at at executiontime. executiontime. called called dynamic dynamic binding binding or or late late binding binding Note Note only only s s super super can can be be called called via via super super variable variable get get type type each each in in employees employees array array for for int int j j 0; 0; j j < < employees.length; employees.length; j j System.out.printf System.out.printf %d %d a a %s\n, %s\n, j, j, employees[ employees[ j j ].getclass.getname ].getclass.getname ; ; dplay dplay whos whos employee[j] employee[j] Dr. S. HAMMAMI main main PayrollSystemTest PayrollSystemTest

final Methods and Classes

final Methods and Classes 1 2 OBJECTIVES In this chapter you will learn: The concept of polymorphism. To use overridden methods to effect polymorphism. To distinguish between abstract and concrete classes. To declare abstract methods

More information

Object-Oriented Programming: Polymorphism

Object-Oriented Programming: Polymorphism Object-Oriented Programming: Polymorphism By Harvey M. Deitel and Paul J. Deitel Jun 1, 2009 Sample Chapter is provided courtesy of Prentice Hall 10.1 Introduction We now continue our study of object-oriented

More information

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

Object-Oriented Programming: Polymorphism Pearson Education, Inc. All rights reserved. 1 10 Object-Oriented Programming: Polymorphism 2 A Motivating Example Employee as an abstract superclass. Lots of different types of employees (well, 4). Executing the same code on all different types

More information

Object Oriented Programming with C++ (24)

Object Oriented Programming with C++ (24) Object Oriented Programming with C++ (24) Zhang, Xinyu Department of Computer Science and Engineering, Ewha Womans University, Seoul, Korea zhangxy@ewha.ac.kr Polymorphism (II) Chapter 13 Outline Review

More information

WEEK 13 EXAMPLES: POLYMORPHISM

WEEK 13 EXAMPLES: POLYMORPHISM WEEK 13 EXAMPLES: POLYMORPHISM CASE STUDY: PAYROLL SYSTEM USING POLYMORPHISM Use the principles of inheritance, abstract class, abstract method, and polymorphism to design a payroll project for a car lot.

More information

Object-Oriented Programming: Polymorphism

Object-Oriented Programming: Polymorphism 10 One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them. John Ronald Reuel Tolkien General propositions do not decide concrete cases. Oliver Wendell

More information

Polymorphism (Deitel chapter 10) (Old versions: chapter 9)

Polymorphism (Deitel chapter 10) (Old versions: chapter 9) Polymorphism (Deitel chapter 10) (Old versions: chapter 9) 1 2 Plan Introduction Relationships Among Objects in an Inheritance Hierarchy Polymorphism Examples Abstract Classes and Methods Example: Inheriting

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Polymorphism Lecture 8 September 28/29, 2004 Introduction 2 Polymorphism Program in the general Derived-class object can be treated as base-class object is -a

More information

Chapter 4. Inheritance

Chapter 4. Inheritance Chapter 4 Inheritance CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Dr. S. HAMMAMI Objectives In In this this chapter you you will will learn

More information

C++ Polymorphism. Systems Programming

C++ Polymorphism. Systems Programming C++ Polymorphism Systems Programming C++ Polymorphism Polymorphism Examples Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class Objects Aiming Derived-Class

More information

Inheritance Introduction. 9.1 Introduction 361

Inheritance Introduction. 9.1 Introduction 361 www.thestudycampus.com Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship Between Superclasses and Subclasses 9.4.1 Creating and Using a CommissionEmployee

More information

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Chapter 13 Inheritance and Polymorphism CS 180 Sunil Prabhakar Department of Computer Science Purdue University Introduction Inheritance and polymorphism are key concepts of Object Oriented Programming.

More information

Cpt S 122 Data Structures. Inheritance

Cpt S 122 Data Structures. Inheritance Cpt S 122 Data Structures Inheritance Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Base Classes & Derived Classes Relationship between

More information

Announcements. Final exam. Course evaluations. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA

Announcements. Final exam. Course evaluations. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA Announcements Final exam Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA Course evaluations Wednesday November 28th Need volunteer to collect evaluations and deliver them to LWSN. 1 Chapter 13 Inheritance

More information

Computer Programming Inheritance 10 th Lecture

Computer Programming Inheritance 10 th Lecture Computer Programming Inheritance 10 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved 순서 Inheritance

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab

More information

Object- Oriented Programming: Inheritance

Object- Oriented Programming: Inheritance 9 Say not you know another entirely, till you have divided an inheritance with him. Johann Kasper Lavater This method is to define as the number of a class the class of all classes similar to the given

More information

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Inheritance A form of software reuse in which a new class is created by absorbing an existing class s members and enriching them with

More information

BBM 102 Introduction to Programming II Spring Inheritance

BBM 102 Introduction to Programming II Spring Inheritance BBM 102 Introduction to Programming II Spring 2018 Inheritance 1 Today Inheritance Notion of subclasses and superclasses protected members UML Class Diagrams for inheritance 2 Inheritance A form of software

More information

Solutions for H7. Lecture: Xu Ying Liu

Solutions for H7. Lecture: Xu Ying Liu Lecture: Xu Ying Liu 2011 0 Ex13.12 1 // Exercise 13.12 Solution: Date.h 2 // Date class definition. 3 #ifndef DATE_H 4 #define DATE_H 6 #include 7 using namespace std; 8 9 class Date 10 { 11

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 12 continue 12.6 Case Study: Payroll System Using Polymorphism This section reexamines the CommissionEmployee- BasePlusCommissionEmployee hierarchy that we explored throughout

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED - Polymorphism - Virtual Functions - Abstract Classes - Virtual

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(b): Abstract classes & Polymorphism Lecture Contents 2 Abstract base classes Concrete classes Polymorphic processing Dr. Amal Khalifa,

More information

Chapter 10. Further Abstraction Techniques

Chapter 10. Further Abstraction Techniques Chapter 10 Further Abstraction Techniques In the previous chapter, we saw how Java checks the usage of methods. We also saw that if the method is not defined in the superclass, the compiler will not work.

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(a): Abstract Classes Lecture Contents 2 Abstract base classes Concrete classes Dr. Amal Khalifa, 2014 Abstract Classes and Methods

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance? 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

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE 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

More information

COEN244: Polymorphism

COEN244: Polymorphism COEN244: Polymorphism Aishy Amer Electrical & Computer Engineering Polymorphism means variable behavior / ability to appear in many forms Outline Casting between objects Using pointers to access objects

More information

Chapter 7. Inheritance

Chapter 7. Inheritance Chapter 7 Inheritance Introduction to Inheritance Inheritance is one of the main techniques of objectoriented programming (OOP) Using this technique, a very general form of a class is first defined and

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4(b): Inheritance & Polymorphism Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword

More information

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

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy CISC181 Introduction to Computer Science Dr. McCoy Lecture 27 December 8, 2009 Object Oriented Programming Classes categorize entities that occur in applications. Class teacher captures commonalities of

More information

1.00 Lecture 13. Inheritance

1.00 Lecture 13. Inheritance 1.00 Lecture 13 Inheritance Reading for next time: Big Java: sections 10.5-10.6 Inheritance Inheritance allows you to write new classes based on existing (super or base) classes Inherit super class methods

More information

5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes

5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes Introduction to Polymorphism Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University There are three main programming mechanisms that constitute object-oriented

More information

Banaras Hindu University

Banaras Hindu University Banaras Hindu University A Course on Software Reuse by Design Patterns and Frameworks by Dr. Manjari Gupta Department of Computer Science Banaras Hindu University Lecture 5 Basic Design Patterns Basic

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 4(b): Subclasses and Superclasses OOP OOP - Inheritance Inheritance represents the is a relationship between data types (e.g. student/person)

More information

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August Polymorphism, Dynamic Binding and Interface 2 4 pm Thursday 7/31/2008 @JD2211 1 Announcement Next week is off The class will continue on Tuesday, 12 th August 2 Agenda Review Inheritance Abstract Array

More information

9/10/2018 Programming Data Structures Inheritance

9/10/2018 Programming Data Structures Inheritance 9/10/2018 Programming Data Structures Inheritance 1 Email me if the office door is closed 2 Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

Answer ALL Questions. Each Question carries ONE Mark.

Answer ALL Questions. Each Question carries ONE Mark. SECTION A (10 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (5 Marks) i. Which of the following is not a valid primitive type : a. char b. double c. int

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 4: OO Principles - Polymorphism http://courses.cs.cornell.edu/cs2110/2018su Lecture 3 Recap 2 Good design principles.

More information

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes Java Curriculum for AP Computer Science, Student Lesson A20 1 STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes INTRODUCTION:

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Polymorphism 1 / 19 Introduction to Object-Oriented Programming Today we ll learn how to combine all the elements of object-oriented programming in the design of a program that handles a company payroll.

More information

CS112 Lecture: Inheritance and Polymorphism

CS112 Lecture: Inheritance and Polymorphism CS112 Lecture: Inheritance and Polymorphism Last revised 4/10/08 Objectives: 1. To review the basic concept of inheritance 2. To introduce Polymorphism. 3. To introduce the notions of abstract methods,

More information

Inheritance (continued) Inheritance

Inheritance (continued) Inheritance Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Create a Java project named week9

Create a Java project named week9 Objectives of today s lab: Through this lab, students will explore a hierarchical model for object-oriented design and examine the capabilities of the Java language provides for inheritance and polymorphism.

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 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

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

CST242 Object-Oriented Programming Page 1

CST242 Object-Oriented Programming Page 1 CST4 Object-Oriented Programming Page 4 5 6 67 67 7 8 89 89 9 0 0 0 Objected-Oriented Programming: Objected-Oriented CST4 Programming: CST4 ) Programmers should create systems that ) are easily extensible

More information

Classes and Inheritance Extending Classes, Chapter 5.2

Classes and Inheritance Extending Classes, Chapter 5.2 Classes and Inheritance Extending Classes, Chapter 5.2 Dr. Yvon Feaster Inheritance Inheritance defines a relationship among classes. Key words often associated with inheritance are extend and implements.

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 6 : Abstraction Lecture Contents 2 Abstract classes Abstract methods Case study: Polymorphic processing Sealed methods & classes

More information

Java How to Program, 8/e

Java How to Program, 8/e Java How to Program, 8/e Polymorphism Enables you to program in the general rather than program in the specific. Polymorphism enables you to write programs that process objects that share the same superclass

More information

Object Oriented Relationships

Object Oriented Relationships Lecture 3 Object Oriented Relationships Group home page: http://groups.yahoo.com/group/java CS244/ 2 Object Oriented Relationships Object oriented programs usually consisted of a number of classes Only

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: has a CSC Employee. Supervisor CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog

More information

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Lecture 36: Cloning Last time: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Today: 1. Project #7 assigned 2. equals reconsidered 3. Copying and cloning 4. Composition 11/27/2006

More information

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: has a CSE143 Sp Student. CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

CST141--Inheritance 2

CST141--Inheritance 2 - employeeid - hoursworked - checknumber payrate -+ employeeid setchecknumber() -+ hoursworked setemployeeid() -+ payrate sethoursworked() ed-oriented and Inheritance + setchecknumber() setpayrate() CST141

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Motivating

More information

Inheritance, Polymorphism, and Interfaces

Inheritance, Polymorphism, and Interfaces Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)

More information

[ L5P1] Object-Oriented Programming: Advanced Concepts

[ L5P1] Object-Oriented Programming: Advanced Concepts [ L5P1] Object-Oriented Programming: Advanced Concepts Polymorphism Polymorphism is an important and useful concept in the object-oriented paradigm. Take the example of writing a payroll application for

More information

www.thestudycampus.com inheritance As mentioned in Chapter 3, an important goal of object-oriented programming is code reuse. Just as engineers use components over and over in their designs, programmers

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Software Development With Java CSCI

Software Development With Java CSCI Software Development With Java CSCI-3134-01 D R. R A J S I N G H Outline Week 8 Controlling Access to Members this Reference Default and No-Argument Constructors Set and Get Methods Composition, Enumerations

More information

INTERFACE WHY INTERFACE

INTERFACE WHY INTERFACE INTERFACE WHY INTERFACE Interfaces allow functionality to be shared between objects that agree to a contract on how the software should interact as a unit without needing knowledge of how the objects accomplish

More information

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass Encapsulation Again Inheritance Encapsulation and Inheritance Inf1-OOP Inheritance and Interfaces Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 9, 2015 The Object

More information

Yanbu University College Applied Computer Science (ACS) Introduction to Computer Science (CS 102) Lab Exercise 10

Yanbu University College Applied Computer Science (ACS) Introduction to Computer Science (CS 102) Lab Exercise 10 Yanbu University College BACHELOR OF SCIENCE IN Applied Computer Science (ACS) Introduction to Computer Science (CS 102) Third Semester Academic Year 2011 2012 Lab Exercise 10 Course Instructor: Mohammed

More information

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics Inf1-OOP Inheritance and Interfaces Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Encapsulation Again Inheritance Encapsulation and Inheritance The Object Superclass Flat vs. Nested

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam ( hnam@hanyang.ac.kr ) Lecture 1 1 Data Structures Data? Songs in a smartphone Photos in a camera Files

More information

OVERRIDING. 7/11/2015 Budditha Hettige 82

OVERRIDING. 7/11/2015 Budditha Hettige 82 OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

CPS122 Lecture: Encapsulation, Inheritance, and Polymorphism

CPS122 Lecture: Encapsulation, Inheritance, and Polymorphism Objectives: CPS122 Lecture: Encapsulation, Inheritance, and Polymorphism Last revised January 23, 2015 1. To review the basic concept of inheritance 2. To introduce Polymorphism. 3. To introduce the notions

More information

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information

More information

Chapter 14. Inheritance. Slide 1

Chapter 14. Inheritance. Slide 1 Chapter 14 Inheritance Slide 1 Learning Objectives Inheritance Basics Derived classes, with constructors protected: qualifier Redefining member functions Non-inherited functions Programming with Inheritance

More information

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Comp 249 Programming Methodology Chapter 8 - Polymorphism Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

Inheritance. Chapter 7. Chapter 7 1

Inheritance. Chapter 7. Chapter 7 1 Inheritance Chapter 7 Chapter 7 1 Introduction to Inheritance Inheritance allows us to define a general class and then define more specialized classes simply by adding new details to the more general class

More information

Lecture 4: Extending Classes. Concept

Lecture 4: Extending Classes. Concept Lecture 4: Extending Classes Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class s methods and fields, and

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Introduction to Classes and Objects OBJECTIVES In this chapter you will learn: What classes, objects, methods and instance variables are. How to declare a class and use it to create an object. How to

More information

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

1. Which of the following is the correct expression of character 4? a. 4 b. 4 c. '\0004' d. '4' Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java

More information

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Sung Hee Park Department of Mathematics and Computer Science Virginia State University September 18, 2012 The Object-Oriented Paradigm

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University OOP Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Inheritance

More information

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer public class Base { protected int theint = 100; System.out.println( theint ); public class Doubler extends Base { System.out.println( theint*2 ); public class Tripler extends Base { System.out.println(

More information

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. Inf1-OP Inheritance UML Class Diagrams UML: language for specifying and visualizing OOP software systems UML class diagram: specifies class name, instance variables, methods,... Volker Seeker, adapting

More information

Lecture 5: Inheritance

Lecture 5: Inheritance McGill University Computer Science Department COMP 322 : Introduction to C++ Winter 2009 Lecture 5: Inheritance Sami Zhioua March 11 th, 2009 1 Inheritance Inheritance is a form of software reusability

More information

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with

More information