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

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

Jun 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Programming Language ConceptsObject-Oriented Programming

Janyl Jumadinova28 February, 2017

Page 2: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Three Properties of Object-Oriented Languages:

I Encapsulation

I Inheritance

I Dynamic method binding (polymorphism)

2/19

Page 3: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Encapsulation

I Data and functions bound together into a single object.

I “Data hiding” – hide implementation details from user.

I More accurately, control access to data using public and privatevariables and methods.

3/19

Page 4: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Inheritance

I Hierarchy of classes and objects.

I Shared behaviors and data – code re-use.

I Static polymorphism: one interface for many kinds of object.

4/19

Page 5: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance

I In Java, classes and subclasses form a tree – a class may havemany subclasses, but each subclass extends exactly one parentclass. This is called the class hierarchy.

I Java does not permit “multiple inheritance.”

5/19

Page 6: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance

I On the other hand, the C++ language allows classes to inheritfrom several different parent classes: multiple inheritance.

I For example, consider the following set of classes:

6/19

Page 7: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

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 oftheir parent classes, e.g.,Student(string name, int year, double gpa): Person(name)...

7/19

Page 8: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance in C++

In our example, StudentEmployee can invoke the constructors ofboth parents:

8/19

Page 9: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance in C++

I In our example, assume name is an instance variable in the classPerson, with accessor method getName().

I Then both Student and Employee will inherit this variable aswell as the getName method.

I Now consider the class StudentEmployee. It inherits name andgetName from both Student and Employee.

9/19

Page 10: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance in C++

I In our example, assume name is an instance variable in the classPerson, with accessor method getName().

I Then both Student and Employee will inherit this variable aswell as the getName method.

I Now consider the class StudentEmployee. It inherits name andgetName from both Student and Employee.

9/19

Page 11: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance in C++

I In our example, assume name is an instance variable in the classPerson, with accessor method getName().

I Then both Student and Employee will inherit this variable aswell as the getName method.

I Now consider the class StudentEmployee. It inherits name andgetName from both Student and Employee.

9/19

Page 12: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance

I This is called the “diamond problem” (a.k.a. “Diamond ofDeath”).

I In C++, one way to avoid the error on the previous page is tosimply choose one of the “getName()” methods and ignore theother one.

10/19

Page 13: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance

11/19

Page 14: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Multiple Inheritance

Can we gain the benefits of multiple inheritance in Java?

I Sort of ... in Java we can create “interfaces”.

I They are similar to classes, but an interface has no instancevariables and contains only abstract methods.

I A class can implement more than one interface.

I It is not quite the same as multiple inheritance, but yields manyof the same benefits.

12/19

Page 15: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Dynamic Method Binding

An object’s methods are determined at runtime rather thancompilation time, since subclasses can override methods and can beused wherever the superclass is allowed.

13/19

Page 16: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Dynamic Binding

DynamicDemo.java in the shared repo.

At runtime, Java determined the correct class of the parameter andinvoked ys talk method. This is dynamic method binding.

14/19

Page 17: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Dynamic Binding

DynamicDemo.java in the shared repo.

At runtime, Java determined the correct class of the parameter andinvoked ys talk method. This is dynamic method binding.

14/19

Page 18: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Dynamic Binding

DynamicDemo.java in the shared repo.

At runtime, Java determined the correct class of the parameter andinvoked ys talk method. This is dynamic method binding.

14/19

Page 19: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Static vs. Dynamic Binding

I See program staticbind.cpp in the shared repo.

I There is a parent class Super and a child class Sub, each with aget() 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

Page 20: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Static vs. Dynamic Binding

I By default, C++ uses static binding.

I However, you can still obtain the same behavior as dynamicbinding by using virtual methods and pointers as shown inprogram dynamicbind.cpp

16/19

Page 21: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Overloading

I When two methods in a class have the same name but differentparameters, we say that the method name is “overloaded.”

I This is familiar from Java (where, for instance, we have twodifferent “substring” methods for the String class or multipleconstructor methods).

I In C++ we can even overload symbolic operators like “+” and“*” (really, any operator).

17/19

Page 22: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Overloading

I When two methods in a class have the same name but differentparameters, we say that the method name is “overloaded.”

I This is familiar from Java (where, for instance, we have twodifferent “substring” methods for the String class or multipleconstructor methods).

I In C++ we can even overload symbolic operators like “+” and“*” (really, any operator).

17/19

Page 23: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Overloading an Operator in C++Overload.cpp

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

Page 24: Programming Language Concepts Object-Oriented …Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017

Overloading an Operator in C++

I There are many aspects of operators that we must worry about:precedence, associativity, etc.

I C++ avoids these by forcing overloaded operators to have thesame precedence and associativity that the original operatorshad.

I See program overload2.cpp in the shared repo.

19/19