COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS. Instructor: Prasun Dewan

Size: px
Start display at page:

Download "COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS. Instructor: Prasun Dewan"

Transcription

1 COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS Instructor: Prasun Dewan

2 PREREQUISITE Inheritance 2

3 MORE INHERITANCE Inheritance Graphics Examples Inherited Variables Constructors Memory Representation 3

4 POINT INTERFACE public interface Point { public int getx(); public int gety(); public double getangle(); public double getradius(); Read-only properties defining immutable object! 4

5 CLASS: ACARTESIANPOINT public class ACartesianPoint implements Point { int x, y; public ACartesianPoint(int thex, int they) { x = thex; y = they; public ACartesianPoint(double theradius, double theangle) { x = (int) (theradius*math.cos(theangle)); y = (int) (theradius*math.sin(theangle)); public int getx() { return x; public int gety() { return y; public double getangle() { return Math.atan2(y, x); public double getradius() { return Math.sqrt(x*x + y*y); 5

6 EXTENSION: MUTABLE POINT public interface MutablePoint extends Point { void setx(int newval); void sety(int newval); 6

7 CALLING CONSTRUCTOR IN SUPERCLASS public class AMutablePoint extends ACartesianPoint implements MutablePoint { public AMutablePoint(int thex, int they) { super(thex, they); public void setx(int newval) { x = newval; public void sety(int newval) { y = newval; Superclass constructor Inherited but not available to users of the class if not called from subclass 7

8 BOUNDED POINT public interface BoundedPoint extends MutablePoint { public MutablePoint getupperleftcorner(); public MutablePoint getlowerrightcorner(); public void setupperleftcorner(mutablepoint newval); public void setlowerrightcorner(mutablepoint newval); 8

9 ADDING VARIABLES public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { super(initx, inity); upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); Variables also extended unlike in previous examples Superclass constructor 9

10 BOUNDED POINT IMPLEMENTATION public void setx(int newval) { super.setx(newval); fixx(); public void sety(int newval) { super.sety(newval); fixy(); public MutablePoint getupperleftcorner() { return upperleftcorner; public MutablePoint getlowerrightcorner() { return lowerrightcorner; public void setupperleftcorner(mutablepoint newval) { upperleftcorner = newval; fixx(); fixy(); public void setlowerrightcorner(mutablepoint newval) { lowerrightcorner = newval; fixx(); fixy(); 10

11 NEW CLASS HIERARCHY AND VARIABLES Object int x ACartesianPoint int y AMutablePoint MutablePoint upperleftcorrner ABoundedPoint MutablePoint lowerrightcorrner 11

12 CALLING CONSTRUCTOR IN SUPERCLASS public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { super(initx, inity); upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); Super call 12

13 CALLING CONSTRUCTOR IN SUPERCLASS public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); super(initx, inity); Super call void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); Subclass may want to override initialization in super class Super call must be first statement in constructor but not other methods. Superclass vars initialized before subclass vars, which can be used by thelatter Subclass vars not visible in superclass 13

14 CALLING CONSTRUCTOR IN SUPERCLASS public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { Java complains that no super call y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); Wants Super class variables to be initialized No Super call 14

15 CALLING CONSTRUCTOR IN SUPERCLASS public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { x = initx; y = inity; upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { Manual initialization Java complains that no super call y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); Wants Object variables to be initialized 15

16 MISSING SUPER CALL public class AStringSet extends AStringDatabase { public AStringSet () { public void addelement(string element) { if (member(element)) return; super.addelement(element); No complaints 16

17 JAVA INSERTION public class AStringSet extends AStringDatabase { public AStringSet () { super(); public void addelement(string element) { if (member(element)) return; super.addelement(element); 17

18 MISSING CONSTRUCTOR AND SUPER CALL public class AStringSet extends AStringDatabase { public void addelement(string element) { if (member(element)) return; super.addelement(element); No complaints 18

19 JAVA INSERTION public class AStringSet extends AStringDatabase { public AStringSet () { super(); public void addelement(string element) { if (member(element)) return; super.addelement(element); 19

20 CALLING CONSTRUCTOR IN SUPERCLASS public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { Could it insert the super call? y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); No Super call 20

21 CALLING CONSTRUCTOR IN SUPERCLASS public class ABoundedPoint extends AMutablePoint implements MutablePoint { MutablePoint upperleftcorner, lowerrightcorner; public ABoundedPoint(int initx, int inity, MutablePoint anupperleftcorner, MutablePoint alowerrightcorner) { super(initx, inity); upperleftcorner = anupperleftcorner; lowerrightcorner = alowerrightcorner; fixx(); fixy(); void fixx() { x = Math.max(x, upperleftcorner.getx()); x = Math.min(x, lowerrightcorner.getx()); void fixy() { y = Math.max(y, upperleftcorner.gety()); y = Math.min(y, lowerrightcorner.gety()); Does not know what the actual parameters should be Could initialized them to 0s and null values There may be multiple constructors, which should it choose? 21

22 MEMORY? Object int x ACartesianPoint int y AMutablePoint MutablePoint upperleftcorrner ABoundedPoint MutablePoint lowerrightcorrner 22

23 INHERITANCE AND MEMORY REPRESENTATION public class ACartesianPoint implements Point { int x, y; public class ABoundedPoint extends AMutablePoint implements BoundedPoint { MutablePoint upperleftcorner ; MutablePoint lowerrightcorner; ACartesianPoint@8 8 ACartesianPoint@16 ABoundedPoint@ new ABoundedPoint(75, 75, new AMutablePoint (50,50), new AMutablePoint (100,100)) 23

24 INHERITANCE AND MEMORY REPRESENTATION public class ACartesianPoint implements Point { int x, y; public class ABoundedPoint extends AMutablePoint implements BoundedPoint { int x, y; MutablePoint upperleftcorner ; MutablePoint lowerrightcorner; new ABoundedPoint(75, 75, new AMutablePoint (50,50), new AMutablePoint (100,100)) ACartesianPoint@8 8 ACartesianPoint@16 16 ABoundedPoint@48 Duplicate variables accessed 48 by subclass methods Inherited variables accessed by superclass methods Smalltalk did not allow redeclaration of superclass variables When making a super class out of subclass, accidental redeclaration can happen

25 25

Inheritance and Variables

Inheritance and Variables COMP 401 Prasun Dewan 1 Inheritance and Variables In the previous chapter, we studied the basics of inheritance in the context of collections. Here, we will use a different example to present subtleties

More information

COMP 401 INHERITANCE. Instructor: Prasun Dewan

COMP 401 INHERITANCE. Instructor: Prasun Dewan COMP 401 INHERITANCE Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 INHERITANCE Inheritance Inheriting ancestor s traits Inheriting benefactor s assets Inheriting instance members

More information

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan COMP 401 INHERITANCE: TYPE CHECKING Instructor: Prasun Dewan PREREQUISITE Inheritance 2 TYPE-CHECKING EXAMPLES StringHistory stringhistory = new AStringDatabase(); StringDatabase stringdatabase = new AStringHistory();

More information

COMP 401 COPY: SHALLOW AND DEEP. Instructor: Prasun Dewan

COMP 401 COPY: SHALLOW AND DEEP. Instructor: Prasun Dewan COMP 401 COPY: SHALLOW AND DEEP Instructor: Prasun Dewan PREREQUISITE Composite Object Shapes Inheritance 2 CLONE SEMANTICS? tostring() Object equals() clone() Need to understand memory representation

More information

COMP 401 GRAPHICS. Instructor: Prasun Dewan

COMP 401 GRAPHICS. Instructor: Prasun Dewan COMP 401 GRAPHICS Instructor: Prasun Dewan PREREQUISITE Interfaces 2 MORE ON OBJECTS Graphics types Test-first approach Stubs Physical vs. logical representation Representations with errors 3 MATHEMATICAL

More information

COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan

COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan COMP 401 INITIALIZATION AND INHERITANCE Instructor: Prasun Dewan PREREQUISITE Inheritance Abstract Classes. 2 MORE INHERITANCE 3 AREGULARCOURSE public class ARegularCourse extends ACourse implements Course

More information

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan COMP 401 INHERITANCE: IS-A Instructor: Prasun Dewan PREREQUISITE Interfaces Inheritance and Arrays 2 IS-A IS-A Relationship Human IS-A Mammal Salmon IS-A Fish ACartesianPoint IS-A Point T1 IS-A T2 if T1

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 7. Representation COMP 110 Prasun Dewan 1 Here we take object-based programming to the next level by gaining more experience in defining objects and learning some new concepts. Using the BMI spreadsheet

More information

COMP 401 Fall Recitation 6: Inheritance

COMP 401 Fall Recitation 6: Inheritance COMP 401 Fall 2017 Recitation 6: Inheritance Agenda Brief Review of Inheritance Examples of extending existing classes Exercises and Quiz 2 High-level Classes are Abstract Data Types We can define a set

More information

COMP 401 USER INTERFACE AND ANNOTATIONS. Instructor: Prasun Dewan

COMP 401 USER INTERFACE AND ANNOTATIONS. Instructor: Prasun Dewan COMP 401 USER INTERFACE AND ANNOTATIONS Instructor: Prasun Dewan PREREQUISITES Interfaces 2 INTERACTIVE APP. VS. USER/PROG. INTERFACE public class ABMISpreadsheet implements BMISpreadsheet { double height;

More information

COMP 110 AND 401 CLASS (STATIC) STATE. Instructor: Prasun Dewan

COMP 110 AND 401 CLASS (STATIC) STATE. Instructor: Prasun Dewan COMP 110 AND 401 CLASS (STATIC) STATE Instructor: Prasun Dewan PREREQUISITES State and Properties Interfaces 2 COUNTING INSTANCES OF ACARTESIANPOINT 3 INCREMENTED NUMBER OF INSTANCES 4 CREATING MIDPOINT

More information

JAVA MOCK TEST JAVA MOCK TEST II

JAVA MOCK TEST JAVA MOCK TEST II http://www.tutorialspoint.com JAVA MOCK TEST Copyright tutorialspoint.com This section presents you various set of Mock Tests related to Java Framework. You can download these sample mock tests at your

More information

Collections and Inheritance

Collections and Inheritance COMP 401 Prasun Dewan 1 Collections and Inheritance In the previous chapter, we defined logical structures with fixed number of nodes. In this chapter, we will see how we can use arrays to define types

More information

COMP 401 PATTERNS, INTERFACES AND OBJECTEDITOR. Instructor: Prasun Dewan

COMP 401 PATTERNS, INTERFACES AND OBJECTEDITOR. Instructor: Prasun Dewan COMP 401 PATTERNS, INTERFACES AND OBJECTEDITOR Instructor: Prasun Dewan A TALE OF TWO PIAZZA THREADS In part 1 of Assignment 5, we are required to create a line class that implements the line pattern for

More information

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan

COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan COMP 401 ABSTRACT CLASSES Instructor: Prasun Dewan PREREQUISITE Inheritance 2 TOPICS Top-Down vs. Bottom-Up Inheritance Abstract Classes 3 COURSE DISPLAYER User inputs course title Program displays course

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 25 March 18, 2013 Subtyping and Dynamic Dispatch Announcements HW07 due tonight at midnight Weirich OH cancelled today Help your TAs make the most

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

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

COMP 110/401* Prasun Dewan 1

COMP 110/401* Prasun Dewan 1 7. Graphics COMP 110/401* Prasun Dewan 1 Here we take object-based programming to the next level by gaining more experience in defining objects and learning some new concepts. Using the BMI spreadsheet

More information

23. Generics and Adapters

23. Generics and Adapters COMP 401 Prasun Dewan 1 23. Generics and Adapters The term generic implies unspecialized/common behavior. In an object oriented language, it seems to apply to the class Object, which describes the behavior

More information

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3: PROGRAMMING ASSIGNMENT 3: Read Savitch: Chapter 7 Programming: You will have 5 files all should be located in a dir. named PA3: ShapeP3.java PointP3.java CircleP3.java RectangleP3.java TriangleP3.java

More information

Konzepte von Programmiersprachen

Konzepte von Programmiersprachen Konzepte von Programmiersprachen Chapter 9: Objects and Classes Peter Thiemann Universität Freiburg 9. Juli 2009 Konzepte von Programmiersprachen 1 / 22 Objects state encapsulation (instance vars, fields)

More information

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

More information

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams Testing Object-Oriented Software COMP 4004 Fall 2008 Notes Adapted from Dr. A. Williams Dr. A. Williams, Fall 2008 Software Quality Assurance Lec 9 1 Testing Object-Oriented Software Relevant characteristics

More information

Object-Oriented Concepts

Object-Oriented Concepts JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing

More information

COMP 110 Prasun Dewan 1

COMP 110 Prasun Dewan 1 17. Inheritance COMP 110 Prasun Dewan 1 We have seen how we can create arbitrary sequences, such as histories, using arrays. In this chapter, we will use arrays to create several new kinds of types including

More information

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius;

More information

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150, COMP 401: THE DUAL ROLE OF A CLASS Instructor: Prasun Dewan (FB 150, dewan@unc.edu) SCRIPTS ANALOGY Script Program Follows Follows Theater Performer 2 STRUCTURING IN SCRIPTS Script (Folder) Act (File)

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Compilers CS S-10 Object Oriented Extensions

Compilers CS S-10 Object Oriented Extensions Compilers CS414-2003S-10 Object Oriented Extensions David Galles Department of Computer Science University of San Francisco 10-0: Classes simplejava classes are equivalent to C structs class myclass {

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 25 November 1, 2017 Inheritance and Dynamic Dispatch (Chapter 24) Announcements HW7: Chat Client Available Soon Due: Tuesday, November 14 th at 11:59pm

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

Reusing Classes. Hendrik Speleers

Reusing Classes. Hendrik Speleers Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 March 26, 2015 Inheritance and Dynamic Dispatch Chapter 24 public interface Displaceable { public int getx(); public int gety(); public void move

More information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

COMP 401 Prasun Dewan 1

COMP 401 Prasun Dewan 1 21. More Inheritance COMP 401 Prasun Dewan 1 In this chapter, we will go deeper into inheritance. We will distinguish between bottom-up design of inheritance, which we did in the examples of chapter 17,

More information

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

COMP 401 Prasun Dewan 1

COMP 401 Prasun Dewan 1 21. More Inheritance COMP 401 Prasun Dewan 1 In this chapter, we will go deeper into inheritance. We will distinguish between bottom up design of inheritance, which we did in the examples of chapter 17,

More information

Inheritance (Part 2) Notes Chapter 6

Inheritance (Part 2) Notes Chapter 6 Inheritance (Part 2) Notes Chapter 6 1 Object Dog extends Object Dog PureBreed extends Dog PureBreed Mix BloodHound Komondor... Komondor extends PureBreed 2 Implementing Inheritance suppose you want to

More information

Inheritance (Part 5) Odds and ends

Inheritance (Part 5) Odds and ends Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013 Now Use what we learned about subtyping for records and functions to understand

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Object-Oriented Programming (Java)

Object-Oriented Programming (Java) Object-Oriented Programming (Java) Topics Covered Today 2.1 Implementing Classes 2.1.1 Defining Classes 2.1.2 Inheritance 2.1.3 Method equals and Method tostring 2 Define Classes class classname extends

More information

C++ Programming: Inheritance

C++ Programming: Inheritance C++ Programming: Inheritance 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Basics on inheritance C++ syntax for inheritance protected members Constructors and destructors

More information

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

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

CS S-10 Object Oriented Extensions 1. What do we need to add to simplejava classes to make them true objects?

CS S-10 Object Oriented Extensions 1. What do we need to add to simplejava classes to make them true objects? CS414-2017S-10 Object Oriented Extensions 1 10-0: Classes simplejava classes are equivalent to C structs class myclass { struct mystruct { int x; int x; int y; int y; boolean z; int z; What do we need

More information

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017 Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)

More information

CS Programming I: Inheritance

CS Programming I: Inheritance CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance

More information

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses Objectives Inheritance Students should: Understand the concept and role of inheritance. Be able to design appropriate class inheritance hierarchies. Be able to make use of inheritance to create new Java

More information

Mobile Application Programming. Swift Classes

Mobile Application Programming. Swift Classes Mobile Application Programming Swift Classes Swift Top-Level Entities Like C/C++ but unlike Java, Swift allows declarations of functions, variables, and constants at the top-level, outside any class declaration

More information

2. The object-oriented paradigm

2. The object-oriented paradigm 2. The object-oriented paradigm Plan for this section: Look at things we have to be able to do with a programming language Look at Java and how it is done there Note: I will make a lot of use of the fact

More information

COURSE 2 DESIGN PATTERNS

COURSE 2 DESIGN PATTERNS COURSE 2 DESIGN PATTERNS CONTENT Fundamental principles of OOP Encapsulation Inheritance Abstractisation Polymorphism [Exception Handling] Fundamental Patterns Inheritance Delegation Interface Abstract

More information

COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS. Instructor: Prasun Dewan

COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS. Instructor: Prasun Dewan COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS Instructor: Prasun Dewan A POINTHISTORY IMPLEMENTATION public class APointHistory implements PointHistory { public final int MAX_SIZE = 50; protected Point[]

More information

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism Ibrahim Albluwi Composition A GuitarString has a RingBuffer. A MarkovModel has a Symbol Table. A Symbol Table has a Binary

More information

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004 Type Hierarchy Comp-303 : Programming Techniques Lecture 9 Alexandre Denault Computer Science McGill University Winter 2004 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 1 Last lecture...

More information

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java. Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.

More information

9 Objects and Classes

9 Objects and Classes 9 Objects and Classes Many programming tasks require the program to manage some piece of state through an interface. For example, a file system has internal state, but we access and modify that state only

More information

INHERITANCE - Part 1. CSC 330 OO Software Design 1

INHERITANCE - Part 1. CSC 330 OO Software Design 1 INHERITANCE - Part 1 Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors CSC 330 OO Software Design 1

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

C++ Inheritance and Encapsulation

C++ Inheritance and Encapsulation C++ Inheritance and Encapsulation Private and Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance 1 Private Members Private members

More information

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

An introduction to Java II

An introduction to Java II An introduction to Java II Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/books/tij4 jvo@ualg.pt José Valente de Oliveira 4-1 Java: Generalities A little

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

IT101. Inheritance, Encapsulation, Polymorphism and Constructors IT101 Inheritance, Encapsulation, Polymorphism and Constructors OOP Advantages and Concepts What are OOP s claims to fame? Better suited for team development Facilitates utilizing and creating reusable

More information

Overriding Variables: Shadowing

Overriding Variables: Shadowing Overriding Variables: Shadowing We can override methods, can we override instance variables too? Answer: Yes, it is possible, but not recommended Overriding an instance variable is called shadowing, because

More information

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code: 1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code: public double getsalary() double basesalary = getsalary(); return basesalary + bonus; 3- What does the

More information

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

Inheritance. CSE 142, Summer 2002 Computer Programming 1. Inheritance CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 29-July-2002 cse142-14-inheritance 2002 University of Washington 1 Reading Readings and

More information

Chapter 9 - Object-Oriented Programming: Polymorphism

Chapter 9 - Object-Oriented Programming: Polymorphism Chapter 9 - Object-Oriented Programming: Polymorphism Polymorphism Program in the general Introduction Treat objects in same class hierarchy as if all superclass Abstract class Common functionality Makes

More information

Type Hierarchy. Lecture 6: OOP, autumn 2003

Type Hierarchy. Lecture 6: OOP, autumn 2003 Type Hierarchy Lecture 6: OOP, autumn 2003 The idea Many types have common behavior => type families share common behavior organized into a hierarchy Most common on the top - supertypes Most specific at

More information

INHERITANCE AND EXTENDING CLASSES

INHERITANCE AND EXTENDING CLASSES INHERITANCE AND EXTENDING CLASSES Java programmers often take advantage of a feature of object-oriented programming called inheritance, which allows programmers to make one class an extension of another

More information

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO

More information

Subtyping (Dynamic Polymorphism)

Subtyping (Dynamic Polymorphism) Fall 2018 Subtyping (Dynamic Polymorphism) Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References PFPL - Chapter 24 Structural Subtyping - Chapter 27 Inheritance TAPL (pdf) - Chapter

More information

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada CSCI 101L - Data Structures Practice problems for Final Exam Instructor: Prof Tejada 1 Problem 1. Debug this code Given the following code to increase the value of a variable: void Increment(int x) { x

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

Java. Representing Data. Representing data. Primitive data types

Java. Representing Data. Representing data. Primitive data types Computer Science Representing Data Java 02/23/2010 CPSC 449 161 Unless otherwise noted, all artwork and illustrations by either Rob Kremer or Jörg Denzinger (course instructors) Representing data Manipulating

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits

More information

CS422 - Programming Language Design

CS422 - Programming Language Design 1 CS422 - Programming Language Design Elements of Object-Oriented Programming Grigore Roşu Department of Computer Science University of Illinois at Urbana-Champaign 2 During this and the next lecture we

More information

Java Review: Objects

Java Review: Objects Outline Java review Abstract Data Types (ADTs) Interfaces Class Hierarchy, Abstract Classes, Inheritance Invariants Lists ArrayList LinkedList runtime analysis Iterators Java references 1 Exam Preparation

More information

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

More On inheritance. What you can do in subclass regarding methods: More On inheritance What you can do in subclass regarding methods: The inherited methods can be used directly as they are. You can write a new static method in the subclass that has the same signature

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

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan COMP 110/401 COLLECTION KINDS Instructor: Prasun Dewan PREREQUISITE Arrays Collections Implementation 2 COLLECTION TYPES StringHistory, StringDatabase, StringSet Array ArrayList, List Map Stack Queue 3

More information

Inheritance and Encapsulation. Amit Gupta

Inheritance and Encapsulation. Amit Gupta Inheritance and Encapsulation Amit Gupta Project 1 How did it go? What did you like about it? What did you not like? What can we do to help? Suggestions Ask questions if you don t understand a concept

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

COMP 401 COMMAND OBJECTS AND UNDO. Instructor: Prasun Dewan

COMP 401 COMMAND OBJECTS AND UNDO. Instructor: Prasun Dewan COMP 401 COMMAND OBJECTS AND UNDO Instructor: Prasun Dewan PREREQUISITES Animation Threads Commands 2 TOPICS Command Object Object representing an action invocation such as Do your homework. Threads Support

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

Method Slots: Supporting Methods, Events, and Advices by a Single Language Construct

Method Slots: Supporting Methods, Events, and Advices by a Single Language Construct Method Slots: Supporting Methods, Events, and Advices by a Single Language Construct YungYu Zhuang and Shigeru Chiba The University of Tokyo More and more paradigms are supported by dedicated constructs

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

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

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

RECITATION 4. Casting and graphics (with ObjectEditor)

RECITATION 4. Casting and graphics (with ObjectEditor) RECITATION 4 Casting and graphics (with ObjectEditor) CASTING Changing the type of one variable into another type Cast a variable by adding the new type in parentheses before the variable Example: int

More information

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance? INHERITANCE - Part 1 OOP Major Capabilities Introduction Basic Concepts and Syntax Protected Members Constructors and Destructors Under Inheritance Multiple Inheritance Common Programming Errors encapsulation

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

CSE 142 Sample Final Exam #2

CSE 142 Sample Final Exam #2 CSE 142 Sample Final Exam #2 1. Array Mystery Consider the following method: public static int arraymystery(int[] array) { int x = 0; for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i

More information