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

Similar documents
Java Object Oriented Design. CSC207 Fall 2014

COMP322 - Introduction to C++

Inheritance, and Polymorphism.

CS-202 Introduction to Object Oriented Programming

Polymorphism. Arizona State University 1

Chapter 5 Object-Oriented Programming

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

Inheritance -- Introduction

What is Inheritance?

CS105 C++ Lecture 7. More on Classes, Inheritance

CMSC 132: Object-Oriented Programming II. Inheritance

CIS 110: Introduction to computer programming

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

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

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

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

Chapter 7. Inheritance

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Polymorphism. Zimmer CSCI 330

ECE 122. Engineering Problem Solving with Java

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

Week 5-1: ADT Design

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

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

Programming Exercise 14: Inheritance and Polymorphism

C++ Important Questions with Answers

ENCAPSULATION AND POLYMORPHISM

Inheritance and Encapsulation. Amit Gupta

OVERRIDING. 7/11/2015 Budditha Hettige 82

Data Abstraction. Hwansoo Han

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

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

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

Object Oriented Programming: Based on slides from Skrien Chapter 2

VIRTUAL FUNCTIONS Chapter 10

COP 3530 Discussion Session #2 Ferhat Ay

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

PROGRAMMING LANGUAGE 2

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

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

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

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

CGS 2405 Advanced Programming with C++ Course Justification

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

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

Chapter 1: Object-Oriented Programming Using C++

JAVA MOCK TEST JAVA MOCK TEST II

What are the characteristics of Object Oriented programming language?

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

CS313D: ADVANCED PROGRAMMING LANGUAGE

Practice for Chapter 11

CSC1322 Object-Oriented Programming Concepts

Implements vs. Extends When Defining a Class

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Object Oriented Software Design II

Overriding Variables: Shadowing

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Software Design II

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 4 Defining Classes I

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

Lecture 5: Inheritance

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

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

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Object-Oriented Programming (OOP) Fundamental Principles of OOP

C08: Inheritance and Polymorphism

JAVA MOCK TEST JAVA MOCK TEST III

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

CS250 Final Review Questions

CS260 Intro to Java & Android 03.Java Language Basics

COP 3330 Final Exam Review

C08: Inheritance and Polymorphism

Darrell Bethea June 6, 2011

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

15CS45 : OBJECT ORIENTED CONCEPTS

Super-Classes and sub-classes

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Arrays Classes & Methods, Inheritance

Programming overview

Inheritance and Polymorphism

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

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Inheritance and Polymorphism

G Programming Languages - Fall 2012

Programming II (CS300)

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

What does it mean by information hiding? What are the advantages of it? {5 Marks}

QUESTIONS FOR AVERAGE BLOOMERS

Lecture 18 CSE11 Fall 2013 Inheritance

CompuScholar, Inc. 9th - 12th grades

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

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

Inheritance, Polymorphism, and Interfaces

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

3. Object-Oriented Databases

CS111: PROGRAMMING LANGUAGE II

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

Transcription:

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

Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism) 2/19

Encapsulation Data and functions bound together into a single object. Data hiding hide implementation details from user. More accurately, control access to data using public and private variables and methods. 3/19

Inheritance Hierarchy of classes and objects. Shared behaviors and data code re-use. Static polymorphism: one interface for many kinds of object. 4/19

Multiple Inheritance In Java, classes and subclasses form a tree a class may have many subclasses, but each subclass extends exactly one parent class. This is called the class hierarchy. Java does not permit multiple inheritance. 5/19

Multiple Inheritance On the other hand, the C++ language allows classes to inherit from several different parent classes: multiple inheritance. For example, consider the following set of classes: 6/19

Multiple Inheritance in C++ class Person {... }; class Student : public Person {... }; class Employee : public Person {... }; class StudentEmployee : public Student, public Employee {...}; In C++, constructors for subclasses can invoke the constructors of their parent classes, e.g., Student(string name, int year, double gpa): Person(name)... 7/19

Multiple Inheritance in C++ In our example, StudentEmployee can invoke the constructors of both parents: 8/19

Multiple Inheritance in C++ In our example, assume name is an instance variable in the class Person, with accessor method getname(). Then both Student and Employee will inherit this variable as well as the getname method. 9/19

Multiple Inheritance in C++ In our example, assume name is an instance variable in the class Person, with accessor method getname(). Then both Student and Employee will inherit this variable as well as the getname method. Now consider the class StudentEmployee. It inherits name and getname from both Student and Employee. 9/19

Multiple Inheritance in C++ In our example, assume name is an instance variable in the class Person, with accessor method getname(). Then both Student and Employee will inherit this variable as well as the getname method. Now consider the class StudentEmployee. It inherits name and getname from both Student and Employee. 9/19

Multiple Inheritance This is called the diamond problem (a.k.a. Diamond of Death ). In C++, one way to avoid the error on the previous page is to simply choose one of the getname() methods and ignore the other one. 10/19

Multiple Inheritance 11/19

Multiple Inheritance Can we gain the benefits of multiple inheritance in Java? Sort of... in Java we can create interfaces. They are similar to classes, but an interface has no instance variables and contains only abstract methods. A class can implement more than one interface. It is not quite the same as multiple inheritance, but yields many of the same benefits. 12/19

Dynamic Method Binding An object s methods are determined at runtime rather than compilation time, since subclasses can override methods and can be used wherever the superclass is allowed. 13/19

Dynamic Binding 14/19

Dynamic Binding DynamicDemo.java in the shared repo. 14/19

Dynamic Binding DynamicDemo.java in the shared repo. At runtime, Java determined the correct class of the parameter and invoked ys talk method. This is dynamic method binding. 14/19

Static vs. Dynamic Binding See program staticbind.cpp in the shared repo. There is a parent class Super and a child class Sub, each with a get() method. Sub a(10,20); Super b = a; cout << a.get() << endl; // Which "get"? cout << b.get() << endl; // Which "get"? Both will use Super s get() method! 15/19

Static vs. Dynamic Binding By default, C++ uses static binding. However, you can still obtain the same behavior as dynamic binding by using virtual methods and pointers as shown in program dynamicbind.cpp 16/19

Overloading When two methods in a class have the same name but different parameters, we say that the method name is overloaded. 17/19

Overloading When two methods in a class have the same name but different parameters, we say that the method name is overloaded. This is familiar from Java (where, for instance, we have two different substring methods for the String class or multiple constructor methods). In C++ we can even overload symbolic operators like + and * (really, any operator). 17/19

Overload.cpp Overloading an Operator in C++ class Pirate { public: Pirate(string name) { this->name = name;} Pirate operator +(Pirate p) { return Pirate(p.getName() + name); }... some code omitted...... Pirate x("fred"); Pirate y("mary"); Pirate a = x + y; // Creates a Pirate named "MaryFred" See overload.cpp and overload1.cpp in the shared repo. 18/19

Overloading an Operator in C++ There are many aspects of operators that we must worry about: precedence, associativity, etc. C++ avoids these by forcing overloaded operators to have the same precedence and associativity that the original operators had. See program overload2.cpp in the shared repo. 19/19