COS 333: Chapter 12, Part 2

Willem van Heerden
17 Nov 202158:03

Summary

TLDRThis lecture delves into object-oriented programming (OOP) languages, focusing on C++, Java, C#, and Ruby. It explores the design issues and characteristics of each language, including C++'s efficiency and hybrid nature, Java's object-oriented purity and memory management, C#'s class and struct distinctions, and Ruby's dynamic and flexible OOP approach. The lecture also covers access controls, inheritance, polymorphism, and the unique features of each language, providing a comprehensive understanding of OOP in different programming environments.

Takeaways

  • 📘 C++ is a hybrid language that supports both imperative and object-oriented programming paradigms, inheriting much of its syntax from C.
  • 🔒 C++ uses access controls like public, private, and protected to manage the visibility of class members, with specific rules for inheritance to maintain encapsulation.
  • 🔄 C++ supports multiple inheritance, which allows a class to inherit from more than one parent class, but this can lead to complexities in member access.
  • 🚀 C++ is designed with efficiency in mind, offering fast execution speeds, especially when compared to languages like Smalltalk.
  • 🔄 Java is more object-oriented than C++ and requires all data to be in the form of objects, with wrapper classes for primitive data types.
  • 🗑️ Java manages memory through a garbage collector, relieving the programmer from manual memory deallocation, unlike C++ which requires manual management.
  • 🔗 Java supports single inheritance and interfaces to simulate multiple inheritance without the complexities associated with true multiple inheritance.
  • 📝 C# differs from Java in that it includes structs in addition to classes, with structs being stack-allocated and not supporting inheritance or self-referencing.
  • 📜 Ruby is a pure object-oriented language where everything is an object, and it shares similarities with Smalltalk in terms of message passing and dynamic nature.
  • 🔄 Ruby allows for dynamic changes to class and method definitions during runtime, offering flexibility but potentially complicating maintenance and predictability.
  • 🛑 Ruby does not support abstract classes or direct multiple inheritance but uses modules to provide a form of multiple inheritance through mixing functions into classes.

Q & A

  • What programming language is considered a hybrid language and why?

    -C++ is considered a hybrid language because it has a mixed type system, inheriting an imperative type system from C and adding an object-oriented class system, allowing for both procedural and object-oriented programming to be used interchangeably within the same program.

  • What are the three types of access controls in C++ for class members?

    -The three types of access controls in C++ for class members are public, private, and protected. Public members are accessible to subclasses and client code, private members are only visible within the class and to friends, and protected members are visible to the class, friends, and subclasses but not to client code.

  • How does private inheritance in C++ differ from public inheritance?

    -Private inheritance in C++ makes the public and protected members of the parent class private in the derived class, effectively disallowing the derived class from being a subtype of the parent class. Public inheritance, on the other hand, does not change the access controls of the inherited members, allowing the derived class to maintain the same behavior as the parent class.

  • What is the default message binding in C++ and why is it the default?

    -The default message binding in C++ is static message binding. It is the default because it is much faster than dynamic binding, which aligns with C++'s design focus on efficiency.

  • What is the difference between a class and a struct in C#?

    -In C#, a class is a reference type that can be allocated on the heap and supports inheritance, while a struct is a value type that is stack-allocated and does not support inheritance. Structs are less powerful than classes and cannot reference themselves, making them unsuitable for creating complex data structures like linked lists or trees.

  • Why is Java's approach to message binding simpler than C++'s?

    -Java's approach to message binding is simpler because almost all message binding in Java is dynamic by default, requiring no special keywords for virtual methods as in C++. Static binding in Java only occurs with final, private, or static methods, which are the only cases where methods cannot be overridden.

  • What is the purpose of interfaces in Java and how do they differ from C++'s approach to multiple inheritance?

    -Interfaces in Java provide a way to simulate multiple inheritance by allowing classes to implement multiple interfaces. Unlike C++, which supports true multiple inheritance, Java interfaces only provide method declarations and named constants, without the ability to implement methods or have instance variables, thus avoiding the complexity and potential issues of multiple inheritance.

  • How does Ruby's approach to object-oriented programming differ from the other languages discussed?

    -Ruby's approach differs significantly as it is a more pure object-oriented language where everything is an object, similar to Smalltalk. It supports dynamic message binding with typeless object references, and allows for changes in class and method definitions at runtime, providing a flexible but less static approach compared to languages like C++, Java, and C#.

  • What is the concept of 're-exportation' in C++ and why is it used?

    -Re-exportation in C++ is the process of making a member that is private due to private inheritance visible as if it had been inherited publicly. It is used to overcome the limitation of private inheritance, which changes the access of inherited members to private, by using the scope resolution operator to re-export the member with its original access level.

  • Why is dynamic message binding not required for methods marked as final in Java?

    -Dynamic message binding is not required for final methods in Java because final methods cannot be overridden in derived classes. Since there can't be an overridden method to bind to, static binding is used instead, which is more efficient as it doesn't require the overhead of determining the method to call at runtime.

  • How does Ruby's handling of access control for data and methods differ from C++?

    -In Ruby, all data within a class is private by default and can only be accessed by methods within that class, similar to C++. However, Ruby checks method access at runtime, unlike C++ where access can usually be checked at compile time. This difference is due to Ruby's dynamic nature and the fact that all variables are typeless object references.

Outlines

00:00

📘 Introduction to Object-Oriented Programming in C++

This paragraph introduces the topic of object-oriented programming (OOP) in C++, highlighting its evolution from C and Simula 67. It emphasizes C++'s status as a widely used OOP language and its hybrid nature, combining imperative and object-oriented features. The paragraph explains C++'s access controls for class members (public, private, protected) and how they relate to inheritance, including the visibility of members to subclasses and client code. It also touches on C++'s efficiency and its use of standalone functions, allowing a mix of procedural and object-oriented programming styles.

05:00

🔑 Access Control and Inheritance in C++

The second paragraph delves deeper into access controls in C++, explaining the nuances of public, private, and protected access levels for class members. It discusses how these access levels are affected by inheritance, with public derivation maintaining access levels and private derivation changing inherited members to private, thus preventing subtype polymorphism. The paragraph also introduces protected derivation, which adjusts access levels to protected. It includes a code example illustrating the differences between public and private inheritance and invites viewers to consider the implications of private inheritance.

10:00

🔄 Multiple Inheritance and Message Binding in C++

This paragraph explores C++'s support for multiple inheritance and the complexities it introduces, such as name conflicts between parent classes. It explains the use of the scope resolution operator to disambiguate such conflicts. The paragraph also discusses message binding, emphasizing C++'s flexibility in allowing static or dynamic binding, with static binding being the default for efficiency. It introduces pure virtual functions and abstract classes, illustrating how they are used with a shape example, and explains the difference between dynamic and static binding using a square and rectangle class example.

15:01

🚧 Object Slicing and Java's OOP Paradigms

The fourth paragraph transitions to Java, contrasting its OOP paradigms with C++. It points out Java's lack of support for stack-allocated objects and its use of references instead of pointers. Java's automatic memory management through garbage collection is highlighted, along with the language's single inheritance model and the use of interfaces to simulate multiple inheritance. The paragraph also discusses Java's final methods and nested classes, explaining how they differ from those in C++.

20:02

🔄 Java's Dynamic Message Binding and Interface Usage

This paragraph focuses on Java's dynamic message binding, which is the default for almost all method calls, except for final, private, and static methods. It discusses the implications of this for Java's performance compared to C++. The paragraph also explains the use of interfaces in Java, how they differ from abstract classes, and the benefits they provide over C++'s multiple inheritance, including simplicity and the avoidance of diamond inheritance problems.

25:04

🌐 C#'s OOP Features and Differences from Java

The sixth paragraph introduces C#, noting its similarities to Java but also its unique features, such as structs. It discusses C#'s class and struct syntax, inheritance, and the use of 'new' and 'override' keywords for explicit method overriding. The paragraph also covers C#'s support for interfaces, nested classes, and the root object class from which all C# classes inherit, drawing parallels and contrasts with Java's OOP model.

30:05

💠 Ruby's Dynamic OOP Nature and Modules

The seventh paragraph presents Ruby, a language with a different OOP philosophy, more aligned with Smalltalk. It discusses Ruby's dynamic nature, where classes and methods can be redefined during execution, and all data is private, accessible only through methods. The paragraph explains Ruby's use of modules as a way to simulate multiple inheritance and the lack of abstract classes. It also touches on Ruby's dynamic message binding and the implications of its fully dynamic OOP model.

35:07

🎓 Conclusion and Exam Preparation Advice

The final paragraph concludes the lecture series, offering advice for exam preparation. It encourages students to study both the slides and the textbook, wishing them luck in their studies. This paragraph serves as a sign-off, summarizing the lecture content and providing guidance for further learning.

Mindmap

Keywords

💡Object-Oriented Programming (OOP)

Object-Oriented Programming is a programming paradigm that uses 'objects' to design applications and software. These objects are instances of 'classes', which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). In the video, OOP is the central theme, with a focus on how different programming languages like C++, Java, C#, and Ruby implement OOP concepts.

💡C++

C++ is a hybrid programming language that supports both procedural and object-oriented programming paradigms. It evolved from the C language and added support for classes and objects with access controls like public, private, and protected. The video discusses C++'s object-oriented features, including inheritance, polymorphism, and the use of access specifiers.

💡Java

Java is an object-oriented programming language that is influenced by C++. Unlike C++, Java enforces a more strict OOP model where all data is treated as objects and there is no support for multiple inheritance. The script mentions Java's use of interfaces to simulate multiple inheritance and its automatic memory management through garbage collection.

💡C#

C#, pronounced 'C-sharp', is a language that also follows the object-oriented paradigm and is influenced by both C++ and Java. It introduces the concept of structs, which are value types different from classes, and enforces explicit marking of overridden methods with the 'override' keyword. The video script explains how C# handles inheritance and method overriding differently from C++.

💡Ruby

Ruby is a dynamic, object-oriented programming language that is more 'pure' in its OOP approach compared to C++, Java, and C#. It treats everything as an object and allows for dynamic changes to class definitions and method implementations. The script highlights Ruby's unique approach to OOP, including its use of modules to simulate multiple inheritance.

💡Inheritance

Inheritance is a fundamental concept in OOP that allows a new class, known as a 'subclass' or 'derived class', to inherit attributes and methods from an existing class, known as the 'parent class' or 'superclass'. The video script discusses how inheritance is implemented differently across C++, Java, C#, and Ruby, including access control changes and the use of interfaces in Java.

💡Polymorphism

Polymorphism is the ability of different data types to respond to the same method call in different ways. It is often associated with the use of 'virtual' functions in C++ and is a core concept that allows for more flexible and reusable code. The script explains polymorphism in the context of dynamic message binding and the use of 'virtual' keywords in C++.

💡Access Control

Access control in OOP is the restriction of access to an object's components based on their exposure to the external interface. The video script describes how different access levels (public, private, protected) are used in C++ to control the visibility of class members and how this concept is applied in other languages like Java and Ruby.

💡Interface

An interface in OOP is a collection of abstract method declarations, without implementation, that a class can implement. The script discusses Java's use of interfaces to provide a form of multiple inheritance, requiring any class that implements an interface to provide implementations for its methods.

💡Dynamic Message Binding

Dynamic message binding, also known as runtime binding, is the process of linking a method call to the method implementation at runtime rather than at compile time. The video script explains how dynamic message binding is used in C++, Java, and Ruby to achieve polymorphic behavior.

💡Abstract Class

An abstract class is a class that cannot be instantiated and may contain one or more abstract methods, which have no implementation. The script discusses abstract classes in the context of C++ and how they are used to define a base class from which other classes may inherit.

Highlights

C++ evolved from C and Simula 67, inheriting C's syntax and extending Simula's object model into a full object-oriented system.

C++ is a hybrid language, combining both imperative and object-oriented programming paradigms.

C++ supports access controls like public, private, and protected for class members, influencing how they are inherited and accessed.

In C++, inheritance can have access specifiers that alter the visibility of inherited members in subclasses.

C++ allows for standalone functions outside of classes, enabling procedural programming alongside object-oriented approaches.

Java is more object-oriented than C++, with all data taking the form of objects and a class hierarchy rooted in an 'Object' class.

Java handles memory management automatically through garbage collection, unlike C++ which requires manual memory management.

Java supports interfaces, offering a limited form of multiple inheritance without the associated drawbacks.

C# differs from Java in its support for structs in addition to classes, with structs being stack-allocated and not supporting inheritance.

C# requires the use of 'new' and 'override' keywords for method overriding, making the language more explicit about these actions.

Ruby is a pure object-oriented language where everything is an object, similar to Smalltalk, and supports dynamic message binding.

Ruby allows for class and method definitions to be changed at runtime, providing flexibility but potentially affecting maintainability.

Ruby uses modules to simulate multiple inheritance, with functions from a module mixed into a class but requiring explicit module naming during calls.

In Ruby, all data is private by default, emphasizing encapsulation, and method access is checked at runtime due to dynamic typing.

Ruby does not support abstract classes, and all classes can be instantiated, differing from languages like C++.

Transcripts

play00:00

welcome to the final lecture for this

play00:03

course where we'll be dealing with the

play00:05

second part of chapter 12 in which we'll

play00:09

continue looking at object-oriented

play00:12

programming

play00:15

in today's lecture we'll continue

play00:17

looking at examples of object-oriented

play00:20

programming languages

play00:22

and we'll discuss each in terms of the

play00:25

design issues related to object-oriented

play00:28

programming that we introduced in the

play00:30

last lecture we'll specifically be

play00:33

looking at c plus java c sharp and ruby

play00:38

in this lecture

play00:42

so we'll begin our discussion by looking

play00:44

at c plus plus

play00:46

firstly some general characteristics

play00:48

related to the programming language so c

play00:51

plus evolved from two main languages

play00:54

c and similar 67

play00:57

as you should know by now c is a purely

play01:01

imperative programming language with no

play01:04

object oriented model at all and c

play01:07

inherits a lot of c's syntax

play01:10

you should also recall from earlier on

play01:12

in the course that similar 67 was the

play01:15

first programming language to introduce

play01:17

the notion of objects but it didn't have

play01:19

a full object oriented inheritance model

play01:23

so c

play01:24

took this object model from simula 67

play01:27

and extended it into a fully object

play01:30

oriented system

play01:32

now c plus plus is one of the most

play01:35

widely used object oriented programming

play01:37

languages today

play01:39

it's considered a hybrid language for

play01:41

two reasons firstly it has a mixed type

play01:44

system so it has a full imperative type

play01:47

system which is largely inherited from c

play01:50

so it has things like structs unions

play01:53

enumerations and so on but then added to

play01:57

that it also has an object-oriented

play02:00

class system which allows for full

play02:02

object-oriented programming and the

play02:05

two-type systems can work together so

play02:07

you don't have to choose between using

play02:09

one or the other

play02:11

c plus plus also allows functions that

play02:14

are essentially standalone functions in

play02:17

other words they are not part of a class

play02:20

so this allows for a mixture of both

play02:24

procedural programming and object

play02:26

oriented programming with classes and

play02:30

once again these can be mixed in the

play02:32

same program there's no enforced

play02:34

separation between the two

play02:36

now c plus plus primarily was designed

play02:40

with efficiency in mind so it is a very

play02:43

efficient

play02:44

um fast executing object-oriented

play02:47

programming language particularly if we

play02:50

compare it to small talk which we

play02:52

discussed in the previous lecture which

play02:54

is around 10 times slower than

play02:57

equivalent c plus code

play03:01

next we'll look at inheritance in c

play03:04

plus

play03:06

so all members within a c

play03:09

class have access controls so when we're

play03:12

talking about a member we are talking

play03:14

about a member variable in other words a

play03:17

variable that is part of a class we're

play03:20

also talking about methods within the

play03:22

class which in c plus are called member

play03:26

functions of the class

play03:28

so we have three kinds of access

play03:30

controls that can be used for class

play03:32

members in c

play03:34

public private and protected

play03:37

so a public member is visible to

play03:41

sub-classes of the class in which the

play03:43

member exists as well as client code in

play03:47

other words code that is using the class

play03:50

can directly access those members so

play03:53

typically we would use the public

play03:55

modifier for anything that is part of

play03:58

the public interface that client code

play04:00

should be able to use

play04:02

in general we wouldn't usually declare

play04:05

member variables as public because these

play04:08

should be hidden within the class in

play04:12

question

play04:13

next we have private access control so

play04:16

this means that a member is only visible

play04:19

within the class it's not visible to

play04:22

client code and additionally the member

play04:25

is also visible to any friends of the

play04:27

class so recall that a class can define

play04:32

either a function

play04:34

or an entire other class as a friend and

play04:38

then the friend function or friend class

play04:41

will be able to access anything that is

play04:44

private within the class that has

play04:46

granted friendship

play04:48

and then thirdly we have protected

play04:51

access control here a member is visible

play04:54

to the class as well as friends but also

play04:57

then sub classes of the class in which

play05:00

the member occurs so any derived class

play05:04

of the class in which we have a

play05:05

protected member can access that

play05:08

protected member

play05:10

however a protected member is not

play05:13

visible to client code either so

play05:16

protected

play05:18

access control then lies somewhere

play05:20

between public and private

play05:22

it allows part of the inheritance

play05:25

hierarchy to still have access to the

play05:27

protected member however client code is

play05:30

still cut off from it and generally

play05:32

speaking the client code then must go

play05:35

through the public interface of a class

play05:41

now in c plus inheritance itself can

play05:44

also have an axis control attached to it

play05:48

and this will then define potential

play05:51

changes in the access controls of

play05:54

inherited members within subclasses

play05:58

so there are two types that are

play06:00

discussed in the textbook there is a

play06:02

third type that the textbook doesn't

play06:04

discuss

play06:06

so we have public derivation

play06:08

also called public inheritance which is

play06:11

the default kind of inheritance that is

play06:13

typically used in practice then there's

play06:15

private derivation or private

play06:17

inheritance and then thirdly there's

play06:20

also protected derivation or inheritance

play06:23

which the textbook doesn't focus on i'll

play06:26

briefly touch on that in a moment

play06:29

so with public derivation

play06:32

the derived class inherits all of the

play06:35

public and protected members from the

play06:37

parent class and those public and

play06:40

protected members do not have their

play06:44

access controls changed so anything that

play06:46

is public in the parent class is also

play06:50

public in the derived class anything

play06:52

that is protected in the parent class is

play06:55

also protected in the derived class and

play06:58

as i said this is the default kind of

play07:00

inheritance that we typically deal with

play07:03

now private derivation will inherit the

play07:06

public and protected members from the

play07:09

parent class into the derived class so

play07:13

anything that is public or protected in

play07:16

the parent class will appear in the

play07:18

derived class

play07:19

however what happens then to these

play07:22

public and protected members is that

play07:24

they become private in the derived class

play07:28

so this turns out to disallow subclasses

play07:32

from being subtypes in other words the

play07:36

derived class no longer behaves in the

play07:39

same way as the parent class does

play07:42

so what i would like you to do is to

play07:44

pause the video at this point and try to

play07:47

explain why this is the case why does

play07:50

private inheritance disallow subclasses

play07:54

from being subtypes

play07:59

what i would also like you to do at this

play08:01

point is to pause the video and try to

play08:04

think of a situation in which private

play08:07

derivation would be useful in practice

play08:15

right so then the third kind of

play08:17

derivation that i said i'd briefly touch

play08:19

on is protected derivation and this is

play08:22

very similar to private's derivation

play08:24

again the derived class inherits all of

play08:27

the public and protected members from

play08:28

the parent class

play08:30

however instead of changing these

play08:33

members access controls to private they

play08:36

are instead changed to protect it so

play08:39

what i would like you to also do at this

play08:41

point is to pause the video and try to

play08:43

think of how protected derivation will

play08:46

affect subclasses in relation to the

play08:49

parent classes

play08:56

here we have a simple program code

play08:58

example that illustrates the difference

play09:01

between public and private inheritance

play09:03

in c plus class

play09:05

so firstly we have a class called base

play09:08

class which is defined up at the top

play09:10

here and this has a number of member

play09:13

variables so firstly we've got two

play09:15

private member variables a and x then

play09:19

we've got two protected member variables

play09:22

b and y and finally we have two public

play09:26

member variables

play09:27

c and z

play09:29

now we have two classes firstly subclass

play09:33

one and secondly subclass 2 and both of

play09:37

these inherit from base class

play09:40

as you can see

play09:42

by the inclusion of the name of the base

play09:46

class after the name of the class being

play09:50

defined others either subclass 1 or sub

play09:53

class 2.

play09:55

now the difference is that in the case

play09:57

of subclass 1 the inheritance is marked

play10:00

as public and in subclass 2 the

play10:03

inheritance is marked as private

play10:06

so how does inheritance differ then

play10:09

within subclass 1 and subclass 2

play10:13

well firstly for both subclass 1 and

play10:16

subclass 2 everything that is private

play10:19

within the base class is not provided

play10:23

within either of those classes

play10:26

so the only way that subclass 1 or

play10:29

subclass 2 can access the variables a

play10:33

and x

play10:34

is through either the public or the

play10:36

protected interface of the base class so

play10:40

in other words they would have to use

play10:42

either a public or a protected member

play10:45

function in base class in order to

play10:49

access

play10:50

a and x

play10:53

now subclass 1 inherits everything that

play10:56

is protected and public so it will

play10:58

inherit b y

play11:00

c

play11:01

and z

play11:02

and the axis controls for b y c and z

play11:06

will remain exactly the same in subclass

play11:10

1.

play11:11

so b and y will be inherited in subclass

play11:15

1 and they will both still be protected

play11:18

in subclass 1 and c and z will be

play11:21

inherited in subclass 1 and they will

play11:24

also still be public in subclass 1.

play11:28

now in subclass 2 as we saw our

play11:31

inheritance is private so subclass 2

play11:35

again inherits b y c and z in other

play11:39

words everything that is protected and

play11:42

public within the base class however it

play11:46

changes

play11:47

the access controls for b y c and c so

play11:52

that all four of those are now private

play11:55

within subclass 2. so what this means is

play11:59

that subclass 2 is no longer

play12:02

a sub type of base class because it no

play12:06

longer behaves the same way as base

play12:10

class does because it's changed the

play12:13

access controls

play12:14

for those protected and public member

play12:18

variables

play12:21

now generally speaking in practice

play12:24

we wouldn't use private inheritance in c

play12:27

plus

play12:28

as is it's generally not very useful for

play12:31

us to inherit a number of members from

play12:34

the parent class and then just simply

play12:37

make them all private so c plus provides

play12:41

what is called re-exportation

play12:44

and basically what this means is that a

play12:46

member that is private because of

play12:49

private inheritance or private

play12:51

derivation

play12:52

can be re-exported which then makes that

play12:55

member visible as if it had been

play12:58

inherited publicly and we use the scope

play13:02

resolution operator in order to do this

play13:04

which is represented by two colons so

play13:08

here's an example of how we would go

play13:11

about doing this here we have subclass 3

play13:15

and this is a class that inherits from

play13:18

our base class from our previous example

play13:20

and it does so privately so exactly as

play13:23

we saw in our previous example

play13:26

all of the public and protected members

play13:28

in base class are inherited and they

play13:31

become private within subclass 3

play13:35

but now what we can do is we can choose

play13:38

to make the member variable c

play13:41

public again so c was public in base

play13:44

class

play13:45

and then we use this notation where we

play13:48

specify that we are now using the base

play13:51

classes version of the c member variable

play13:55

that re-exports c and makes it public

play13:59

again in subclass 3.

play14:02

now note that the notation that i've

play14:05

used on this slide is a little bit

play14:06

different to the notation in the

play14:08

textbook the textbook

play14:11

books approach does work however it's a

play14:13

deprecated approach so it's not as

play14:15

commonly used anymore so please refer to

play14:19

the code example on this slide when

play14:22

preparing for this material

play14:27

now c plus unlike many object-oriented

play14:30

programming languages does support

play14:32

multiple inheritance and here we have an

play14:35

example of simple multiple inheritance

play14:39

so we have two classes firstly class

play14:42

thread and secondly class drawing and

play14:45

both of these are parent classes we then

play14:48

have a third class called draw thread

play14:51

and this inherits them from two classes

play14:54

namely thread and drawing and we have to

play14:58

specify what kind of inheritance we want

play15:00

in both cases so the inheritance from

play15:03

thread is public and the inheritance

play15:05

from drawing is also public so this

play15:09

means then that draw thread has two

play15:11

parents

play15:12

the class thread and the class drawing

play15:15

and it inherits all of the public and

play15:18

protected members from those two classes

play15:22

now as i mentioned in the previous

play15:24

lecture there's an issue that arises

play15:27

with multiple inheritance if the parent

play15:31

classes have members that have the same

play15:33

names if in this example

play15:35

if the thread and drawing classes both

play15:39

had a member that had exactly the same

play15:42

name in that case if we were to refer to

play15:44

that name in draw thread then the

play15:47

compiler would return an error saying

play15:49

that it doesn't know which class that

play15:52

member belongs to so in that case we

play15:55

need to then explicitly specify which of

play15:57

the members we are referring to

play16:00

and again we use the scope resolution

play16:03

operator in order to do that so we would

play16:06

have to then specifically in this

play16:08

example specify thread colon codon and

play16:11

then the member name or drawing colon

play16:14

colon and then the member name and that

play16:16

would be within the draw thread class

play16:23

now c plus plus also provides the

play16:26

programmer with maximum flexibility when

play16:28

it comes to message binding

play16:31

so it is up to the programmer or

play16:33

developer of a class at the design time

play16:37

of that class to decide whether a member

play16:40

function will use static message binding

play16:44

or dynamic message binding

play16:47

now by default a member function that

play16:50

isn't explicitly stated to use dynamic

play16:53

message binding uses static message

play16:56

binding and this means that that member

play16:59

function will not work through a

play17:02

polymorphic reference in other words a

play17:05

polymorphic pointer

play17:06

now the reason that this is the default

play17:09

in c plus is that it is much faster than

play17:11

dynamic binding and as we saw before c

play17:15

plus was designed for maximum efficiency

play17:18

so the default is static message binding

play17:22

and if dynamic message binding is not

play17:24

required then a programmer will not

play17:26

specify that dynamic message binding is

play17:29

required which means then that the

play17:31

static message binding will be more

play17:33

efficient

play17:34

now dynamic message binding needs to be

play17:37

explicitly specified by the programmer

play17:39

and they do this by using the special

play17:43

word virtual

play17:45

when they define the member function and

play17:49

what this means then is that the virtual

play17:51

member function can be called through

play17:54

polymorphic variables in other words

play17:56

polymorphic pointers

play17:59

and the methods then will be dynamically

play18:02

bound to the messages that call those

play18:06

methods

play18:07

so in that case the message binding then

play18:09

will be dynamic

play18:12

now c plus plus also provides the

play18:14

concept of a pure virtual function and

play18:17

this is an abstract function which we

play18:19

discussed in the previous lecture and

play18:22

this is a virtual function with no

play18:25

definition attached to it so we simply

play18:28

provide then the signature for this

play18:31

virtual function and we specify equals

play18:34

zero with it which indicates that it has

play18:37

no definition it has no body associated

play18:40

with it so then a class that has at

play18:43

least one pure virtual function is

play18:46

considered an abstract class and as we

play18:48

discussed in the previous lecture we

play18:50

cannot instantiate that class

play18:53

also any derived class if that derived

play18:57

class does not override and provide an

play18:59

implementation

play19:01

for that pure virtual function then that

play19:04

derived class will also be abstract in

play19:07

nature

play19:08

so if we want to concretely instantiate

play19:11

a derived class we must provide an

play19:14

implementation of any pure virtual

play19:17

functions that are defined in the

play19:19

parents class

play19:23

the example on this slide illustrates

play19:26

how message binding works in c plus

play19:30

so we have a number of classes in this

play19:31

example first of all we have a class

play19:34

called shape

play19:35

and we see that shape has a public

play19:39

interface and amongst the public member

play19:43

functions we have a member function

play19:46

called draw

play19:48

this

play19:48

receives no parameters and it also has a

play19:51

void return type meaning that it returns

play19:54

nothing

play19:55

now we've marked this as virtual so this

play19:58

means that the draw member function

play20:01

will use dynamic message binding however

play20:05

we've also specified this equals zero

play20:08

and that indicates that this is a pure

play20:12

virtual member function

play20:14

so we see that there is no body that is

play20:17

defined for this draw member function

play20:21

and what this then implies is we cannot

play20:24

create an object of class shape

play20:28

also any class that inherits from class

play20:31

shape

play20:32

must provide an overrided draw method

play20:37

and this method is required if we want

play20:40

to create an object of that class

play20:43

so here we have a class called circle

play20:46

and we can see that this publicly

play20:48

inherits from class

play20:50

shape

play20:51

and we've provided in the public section

play20:53

of this class an overrided version of

play20:56

the draw method you can see it has

play20:58

exactly the same signature as the draw

play21:02

pure virtual function in the shape class

play21:05

so the function is named draw it

play21:08

receives no parameters and it has a void

play21:10

return type however here we do provide

play21:13

an implementation for it so what this

play21:15

now means is that the circle class is a

play21:19

concrete class in other words we can

play21:21

create an object of class circle

play21:25

we have a similar thing with class

play21:27

rectangles a class rectangle publicly

play21:30

inherits from class shape and it also

play21:34

provides a draw member function and

play21:37

again this has an implementation so this

play21:39

means we can create an object of class

play21:42

rectangle and finally we have class

play21:46

square at the bottom over here and

play21:48

square inherits publicly from the

play21:51

rectangle class

play21:53

and we saw that the rectangle class

play21:55

inherits publicly from the shape class

play21:58

and once again the square class then

play22:01

provides a public implementation of the

play22:04

draw member function if it had not

play22:07

provided its own implementation for the

play22:10

draw member function then it would have

play22:12

inherited the draw implemented for its

play22:15

parent class in other words the

play22:17

rectangle class

play22:19

so now let's look at how we can use

play22:21

these classes

play22:23

so the first two variable definitions we

play22:26

have here are quite simple

play22:28

here we have a variable called sq and

play22:31

its type is a square pointer so it is a

play22:35

pointer or a memory address of a square

play22:38

object and we assign to that a new

play22:41

square so that will call the default

play22:43

constructor of the square class we've

play22:46

just seen that the square class

play22:49

does have an implementation for the draw

play22:52

member function and therefore it is not

play22:56

an abstract clause so we can create an

play22:58

object of the square class

play23:01

similarly we can do the same thing for

play23:04

the rectangle class so here we have a

play23:06

variable called rect its type is a

play23:09

rectangle pointer and we set that to a

play23:12

new rectangle and again because the

play23:14

rectangle class actually implements this

play23:17

draw member function we are allowed to

play23:20

create an object of class rectangle now

play23:24

it's important to note that we wouldn't

play23:26

be able to do the same kind of thing

play23:29

with the shape class however we can

play23:32

create a variable that is a shape

play23:35

pointer which is exactly what we do over

play23:37

here so we have ptr shape and its type

play23:41

is a shape point in other words a

play23:43

pointer to a shape object or a memory

play23:47

address of a shape object now note that

play23:49

we haven't initialized this to anything

play23:52

and we wouldn't be able to assign to

play23:54

this a new shape because we can't create

play23:57

an object of class shape however what we

play24:01

can do is we can assign to our shape

play24:04

pointer the variable

play24:07

is q

play24:08

which we saw

play24:09

is a pointer to a square

play24:13

object and this is allowed because we

play24:16

are using our variable ptr shape

play24:19

polymorphically

play24:20

it can have assigned to it an instance

play24:23

of any of its derived classes so we

play24:26

could also assign to ptr shape the

play24:29

variable rate

play24:32

right so we have the nptr shape and it's

play24:35

actually pointing to a square object

play24:38

even though it is a pointed to a shape

play24:43

now we can call the draw member function

play24:47

on ptr shape and notice that we use this

play24:50

arrow notation over here and this is

play24:52

because ptr shape is a pointer so we

play24:55

can't use a dot notation we need to use

play24:58

an arrow notation which means that we

play25:00

de-reference the pointer and then call

play25:03

the draw method on that

play25:05

now because

play25:07

in the

play25:08

shape class

play25:09

which we have seen is the type of the

play25:12

pointer for ptr shape

play25:15

we see that that is a virtual member

play25:19

function this means that this draw

play25:21

method will then be dynamically bound

play25:27

the call will be dynamically bound to

play25:29

the correct implementation of that draw

play25:33

member function so in other words what

play25:36

will happen then is that because ptr

play25:39

shape is pointing to a square object

play25:44

the draw will then be the draw in class

play25:48

square which will be the one that will

play25:51

be called and this happens dynamically

play25:54

at run time so this is why we are

play25:57

talking about dynamic message binding

play26:00

now finally if we look at the wrecked

play26:04

pointer which we saw up here we have

play26:07

simply defined as a point to a rectangle

play26:11

object and we assigned to it a new

play26:14

rectangle object

play26:16

if we now call draw on that and again we

play26:18

need to use this arrow notation because

play26:21

rect is a pointer

play26:24

then draw will be statically bound and

play26:27

this is because the

play26:30

wrecked

play26:31

pointer

play26:32

is actually a pointer to a rectangle so

play26:35

dynamic message binding doesn't have to

play26:38

happen at this point it will simply call

play26:41

the draw member function that has been

play26:44

defined in the rectangle class itself so

play26:48

that will then be statically bound and

play26:51

because static binding is faster than

play26:53

dynamic binding that means that that

play26:57

member function call will be a very

play26:59

quick very efficient call that will take

play27:02

place

play27:06

now in the previous lecture we spoke

play27:08

about the problem of object truncation

play27:12

or object slicing when we deal with

play27:15

objects that have been allocated on the

play27:18

stack and this is definitely a problem

play27:22

with c

play27:23

plus

play27:24

so if objects are allocated from the

play27:26

stack then assignment to a parent class

play27:30

variable will truncate

play27:32

an object of the child class the derived

play27:36

class and also the message binding is no

play27:39

longer dynamic it's always static so

play27:42

let's look at this in terms of this

play27:45

program code example over here here we

play27:47

have two variables so we've got sq which

play27:51

is of type square and we have rect which

play27:54

is of type rectangle now notice that

play27:58

neither of these are pointers and they

play28:02

are not assigned to new objects that

play28:04

have been created from the heap so in

play28:06

other words they do not behave

play28:08

polymorphically the way we saw in the

play28:11

previous example

play28:12

instead both sq and rect are now stack

play28:17

allocated objects

play28:19

now we are allowed to perform this

play28:21

assignment so here we are assigning sq

play28:25

to the variable rect now this is allowed

play28:29

because as we saw on the previous slides

play28:32

example the square class inherits from

play28:36

the rectangle class a square is a kind

play28:40

of rectangle

play28:42

however what happens here is a copy of

play28:45

data members so this isn't a reference

play28:49

reassignment or a reference copy that

play28:52

takes place

play28:53

instead we copy all of the data from the

play28:57

square object that is relevant for the

play29:00

raked object and we copy that across so

play29:03

what this means is if the square class

play29:06

were to define any additional member

play29:09

variables that the rectangle class does

play29:12

not contain then those wouldn't be

play29:15

copied over in this assignment

play29:19

now even more important than this is if

play29:22

we call the draw on our wrecked variable

play29:27

over here which we know is a stack

play29:29

allocated variable then this will

play29:32

statically

play29:33

call

play29:34

the draw member function within the

play29:37

rectangle class

play29:39

even though what we've actually assigned

play29:41

here is a square

play29:44

so this would have obviously have worked

play29:46

differently if we were working with

play29:48

polymorphic pointers if we were working

play29:50

with polymorphic pointers then what

play29:52

would have happened is that the square

play29:54

classes draw member function would have

play29:57

been called but because we're working

play29:59

with stack allocated object the draw

play30:02

that is called is the draw

play30:04

for the type of the object that is that

play30:07

it is called on in other words it is the

play30:10

draw implementation for the type

play30:13

of the wrecked object which is as we've

play30:17

seen here the class rectangle

play30:23

next we'll move on to our discussion on

play30:27

object-oriented programming support

play30:30

within the java programming language

play30:32

now as we've mentioned previously in

play30:35

this course job is fairly closely

play30:37

related to c plus

play30:39

a lot of the features that java supports

play30:42

were inspired by c plus in the first

play30:45

place

play30:46

so through this discussion we'll be

play30:48

focusing on the differences between java

play30:52

and c

play30:53

rather than exhaustively rehashing

play30:55

things that are the same between the two

play30:57

programming languages

play31:02

so we saw in c plus that c plus plus has

play31:06

a full imperative language type model as

play31:10

well as support for object oriented

play31:13

programming now java is somewhat

play31:15

different it's much more object oriented

play31:19

than c

play31:20

c x

play31:21

so all data in java will take the form

play31:24

of objects except for the primitive

play31:27

types things like ins floats doubles

play31:31

booleans and so on

play31:33

now if these primitive types need to be

play31:36

handled as objects then java provides

play31:39

wrapper classes for these primitives

play31:42

types and these just simply install a

play31:45

primitive type variable so for example

play31:47

there is an integer class and this just

play31:50

simply stores an into value within it

play31:54

now in c plus plus classes can be

play31:57

standalone classes they don't

play31:59

necessarily have to form part of the

play32:01

same class hierarchy in java this is

play32:04

different so all objects are part of a

play32:07

class hierarchy and there is an object

play32:10

class right at the root of that

play32:12

hierarchy so every class

play32:15

is a subtype of object in java

play32:19

also all objects are heap dynamic in

play32:22

nature so we have no such concept um as

play32:29

a stack allocated object in java that

play32:32

does not exist they're all heap dynamic

play32:35

and mostly they are allocated using the

play32:38

new special word

play32:40

there they're also referenced through

play32:43

reference variables so they don't use

play32:46

pointers as is the case in c plus and as

play32:49

we've discussed previously in this

play32:51

course references are a much cleaner

play32:53

solution than pointers they eliminate a

play32:56

lot of the problems that are associated

play32:58

with pointers

play33:00

now memory management in c plus plus

play33:02

must all be handled manually so in c

play33:04

plus one must make sure that the class

play33:07

is completely deallocated using its

play33:10

e-structure

play33:11

in java this isn't the case so memory

play33:14

management in java is handled through a

play33:16

garbage collector which automatically

play33:19

cleans up memory when an object is not

play33:22

required anymore

play33:24

now a finalized method is implicitly

play33:27

called when the garbage collector is

play33:29

about to reclaim the object storage and

play33:31

it is possible for the programmer to

play33:34

actually implement this finalized method

play33:36

and perform specialized operations

play33:38

within it however in general practice

play33:41

there isn't any need for a programmer to

play33:44

implement a finalized method

play33:49

inheritance in java is also a bit

play33:52

different to c

play33:54

so java only supports single inheritance

play33:57

it doesn't have true multiple

play33:59

inheritance the way that c plus plus

play34:01

does

play34:02

now java does provide the concept of an

play34:06

interface so an interface is like an

play34:08

abstract class

play34:10

in fact it's like an abstract class in

play34:12

which all of the methods are abstract

play34:17

so this provides some of the benefits of

play34:19

multiple inheritance but not the

play34:21

drawbacks that we saw associated with

play34:24

for example diamond inheritance which we

play34:27

spoke about in the previous lecture

play34:30

so an interface is only allowed to

play34:32

provide method declarations and named

play34:35

constants

play34:36

so here we have an example of an

play34:38

interface in java this is a public

play34:41

interface and the interface is called

play34:44

comparable it's for a generic type t

play34:48

and we've provided inside this interf

play34:51

interface then a method declaration for

play34:54

a method called compare2

play34:56

that returns an integer value it

play34:59

receives a variable of type t

play35:02

and it is public in nature so what this

play35:06

means now is that anything any class

play35:11

that implements the comparable interface

play35:14

must then provide an implementation for

play35:17

the compare to method and that is

play35:21

required

play35:22

and the interface can't provide any

play35:25

implementation for compare2 or in fact

play35:28

any other methods within it and it also

play35:31

doesn't have instance variables so it's

play35:34

kind of a limited sort of multiple

play35:37

inheritance it's not true multiple

play35:39

inheritance because an interface can't

play35:42

implement all of the things that a class

play35:44

can it can't have member variables it

play35:47

can't have implementations for any of

play35:50

its methods

play35:52

so the use of interfaces then

play35:55

sort of simulates a kind of a multiple

play35:57

inheritance but it's not full multiple

play35:59

inheritance and it's much simpler than

play36:02

in c

play36:03

plus so what i would like you to do at

play36:05

this point is to pause the video and try

play36:08

to explain why this use of interfaces is

play36:12

much simpler in java and what kinds of

play36:16

problems will the use of interfaces

play36:18

alleviate in the case of java that we

play36:21

would see in c plus

play36:27

now java also allows methods to be

play36:30

defined as final and a final method

play36:34

cannot be overridden in a derived class

play36:37

so final essentially means that any

play36:40

derived clause

play36:41

must then refer to the final method

play36:46

that implementation cannot be changed in

play36:48

any of the derived classes

play36:54

binding in java is a lot simpler than in

play36:57

c plus

play36:59

so

play37:00

basically almost all message binding in

play37:03

java is dynamic in nature so all

play37:06

messages are dynamically bound to their

play37:09

methods and static binding only takes

play37:11

place in very specific circumstances

play37:15

and these circumstances are only

play37:18

situations when methods cannot be

play37:20

overridden so if a method cannot be

play37:23

overridden then it means that dynamic

play37:25

binding doesn't make sense in any case

play37:28

because there can't be an overridden

play37:30

method that can be bound to so in that

play37:33

case static binding then makes sense and

play37:36

java will automatically assume that the

play37:39

binding will be static in nature so in

play37:41

practice this happens for final methods

play37:45

we've just seen that a final method

play37:47

cannot be overridden in a derived class

play37:50

also a private method cannot be

play37:53

overridden because it is private within

play37:55

the class it is defined in so it doesn't

play37:57

make sense that it could be overridden

play37:59

by a derived class and also any static

play38:03

method in other words a class method

play38:05

that is defined for an entire class

play38:08

cannot be overridden in a derived class

play38:11

so in none of these three cases do we

play38:15

then have dynamic binding taking place

play38:17

static binding will take place for these

play38:20

three cases but in all other cases

play38:22

dynamic binding is assumed so what i

play38:25

would like you to do is to pause the

play38:27

video at this point and try to explain

play38:30

what effect this will have on the cost

play38:33

of java in comparison to c plus plus

play38:40

another interesting way that java

play38:42

differs from c plus plus is that c plus

play38:45

doesn't support nested classes whereas

play38:48

java has a couple of different types of

play38:51

nested classes

play38:52

now in general a nested class is only

play38:56

visible to the nesting class in other

play38:58

words the class that is containing the

play39:01

nested class

play39:03

so in practice a nested class would only

play39:06

be used in a situation where it has very

play39:08

limited functionality that's only

play39:11

applicable to the class within which it

play39:14

resides

play39:19

now it is possible to nest one class

play39:22

inside another within java and i'll call

play39:25

the nested class in a class and the

play39:27

nesting class the outer class so the

play39:30

outer class contains the inner class

play39:34

so the nesting class in other words the

play39:37

outer class can access all of the

play39:40

members in the nested in other words the

play39:43

inner class so anything even

play39:47

anything that is defined as private

play39:49

within the nested the inner class will

play39:52

be accessible to the outer class

play39:55

now there are two kinds of nested

play39:58

classes that can be defined within

play40:01

java if they are listed inside a class

play40:04

firstly we have non-static nested

play40:07

classes which are also referred to as

play40:09

inner classes

play40:11

and these can access all of the members

play40:14

of the nested classes nesting class so

play40:18

in other words the inner class can

play40:21

access all of the members in the outer

play40:24

class

play40:25

now a static nested class is fairly

play40:27

similar the only difference is that it

play40:29

can't access the members of the nesting

play40:32

class in other words the inner class

play40:34

cannot access the members of the outer

play40:38

class

play40:39

now it is also possible for nested

play40:41

classes to be anonymous i won't go into

play40:44

too much detail here on anonymous nested

play40:48

classes but essentially an anonymous

play40:51

nested class doesn't have a name

play40:52

associated with it we simply specify

play40:56

that it is a class

play40:59

which is assigned to a variable and it

play41:02

is

play41:03

inheriting from a specific class

play41:05

implementing a specific interface

play41:07

and we then provide some limited

play41:09

functionality for it and anonymous

play41:12

classes are intended just as single use

play41:15

classes so we will only be creating one

play41:19

object of that class not multiple

play41:22

objects

play41:23

now in the notes for the slide i provide

play41:26

some example code of anonymous nested

play41:29

classes so please refer to that when

play41:32

preparing for the exam

play41:34

now in java it's also possible to nest

play41:37

classes inside methods within a nesting

play41:40

class so this is even more local than

play41:45

a class nested inside of another class

play41:48

this is a nesting inside an actual

play41:51

method so these are referred to in java

play41:54

as local nested classes these classes

play41:58

don't have class access specifiers so we

play42:01

don't specify that they're either

play42:03

private or public this is because

play42:06

private and public don't make sense in

play42:08

this context because the nested class is

play42:11

only visible within the nesting method

play42:16

so the nesting method can then access

play42:18

all of the members within this nested

play42:21

class and we would typically then use

play42:24

these local nested classes in a

play42:26

situation where the class only needs to

play42:29

be used by one specific method and not

play42:32

even the rest of the outer nesting class

play42:40

next we'll move on to c sharp so support

play42:44

for object oriented programming in c

play42:46

sharp is very similar to java and this

play42:49

is because java was one of the big

play42:52

influencing languages of c sharp

play42:56

now what is interesting in c sharp is

play42:58

that c sharp provides both classes and

play43:02

structs java does not have the notion of

play43:05

a struct

play43:06

now classes in c sharp are very similar

play43:10

to classes in java however structs are

play43:13

different

play43:14

now in c plus a struct and a class are

play43:17

almost exactly the same thing

play43:21

it is possible for example to inherit

play43:25

one struct from another it's also

play43:27

possible to have polymorphic pointers

play43:31

two structs and dynamic message binding

play43:34

works with structs the only difference

play43:36

between structs and classes in c plus

play43:38

plus is the default access modifier for

play43:42

members of structs and classes

play43:45

respectively

play43:46

now in c sharp a struct is a very

play43:48

different thing to a class and struct is

play43:52

a lot less powerful than a struct in c

play43:55

plus plus

play43:57

so a struct is strictly a stack dynamic

play44:00

construct structs can only be allocated

play44:03

from the stack

play44:05

they cannot be dynamically allocated

play44:09

from the heap as they could be in c

play44:12

plus

play44:13

also there's no inheritance involved in

play44:16

c-sharp structs so it isn't possible to

play44:19

define a struct that inherits from

play44:22

another struct in the c sharp

play44:25

and finally and probably most

play44:27

importantly

play44:28

no members within a c-sharp struct can

play44:32

reference the struct itself so for

play44:34

example it's not possible to create a

play44:38

node struct in c sharp

play44:40

where the node contains a reference to

play44:43

another node

play44:45

creating therefore a linked list type of

play44:48

structure if you wanted to create that

play44:50

kind of linked structure or something

play44:52

like a tree you cannot use structs in c

play44:55

sharp you must use classes

play44:58

so what i would like you to do at this

play45:00

point is to pause the video and

play45:02

considering the differences between

play45:03

structs and classes in c sharp

play45:07

what do you think structs are intended

play45:10

for in terms of their use and their

play45:14

purpose in c-sharp programs

play45:22

c sharp uses c plus like syntax for

play45:26

defining classes where inheritance is

play45:30

involved

play45:31

now it is of course possible in c sharp

play45:34

if we have a parent class that has a

play45:37

method defined within it to have a

play45:40

derived child class of that parent class

play45:43

and the child class can then override

play45:46

the method that is provided in the

play45:48

parent class however c sharp is a little

play45:51

bit more explicit than c plus is in this

play45:54

regard so the overriding method its

play45:58

definition must be marked with the

play46:00

special word new

play46:02

so what i would like you to do at this

play46:04

point is to pause the video and try to

play46:06

think of an advantage

play46:09

associated with this approach of

play46:12

explicitly marking every overwritten

play46:15

methods definition with new

play46:17

and also can you think of any

play46:19

disadvantages

play46:26

now it is possible to call the parent

play46:29

classes version of an overridden class

play46:32

explicitly

play46:34

and there you use the base prefix so for

play46:38

example if we wanted to explicitly

play46:41

access the draw method in the parent

play46:45

class and not in the current class then

play46:48

we would use base in order to indicate

play46:52

that we are referring to the parent

play46:54

class now c-sharp also supports

play46:57

interfaces and these work in the same

play47:00

way as we explained in java earlier on

play47:03

in this lecture

play47:08

dynamic message binding in c sharp again

play47:12

works in a fairly similar fashion to c

play47:15

plus however once again c sharp is a bit

play47:18

more explicit about this than c plus

play47:21

plus is

play47:22

so if we have dynamic binding of

play47:25

messages to methods then as in c plus we

play47:30

need to in c sharp uh define the base

play47:34

class method and mark it as virtual so

play47:38

this is the same as in c plus

play47:41

however when we implement a virtual

play47:44

method in a derived class in other words

play47:47

a child class then that implemented

play47:50

virtual method must be marked with the

play47:53

special word override so this is not the

play47:55

case in c plus so what i would like you

play47:58

to do at this point is to pause the

play48:00

video and try to think of an advantage

play48:03

of this approach in c sharp and also can

play48:07

you think of any disadvantages

play48:15

now abstract methods in c sharp are

play48:18

marked as abstract so they don't use the

play48:21

pure virtual function notation that we

play48:24

saw in c plus

play48:26

and then also unlike c plus plus all

play48:29

classes in c sharp are derived from a

play48:32

root object class so in this sense c

play48:35

sharp is more similar to java which also

play48:39

has a whole class hierarchy with a

play48:41

single root class

play48:43

in c plus we've seen that every class is

play48:47

independent and doesn't necessarily have

play48:49

to be part of a single class hierarchy

play48:56

like java c-sharp also supports nested

play48:59

classes however support for these nested

play49:03

classes in c sharp is more limited than

play49:06

what we saw in java

play49:08

so a class can be directly nested within

play49:12

an outer nesting class

play49:15

and this nested class the inner class

play49:17

behaves like a static nested class in

play49:21

java so what this means is the outer

play49:23

class can access any of the members

play49:27

within the inner class however the inner

play49:30

class cannot access any of the members

play49:34

of the outer class that contains the

play49:36

nested class

play49:38

there's no

play49:39

nested class structure in c sharp that

play49:43

is similar to the non-static nested

play49:45

classes in java so there's no way for

play49:50

this in a nested class to have access to

play49:53

the members of the outer

play49:57

containing class

play50:01

the last object-oriented programming

play50:04

language that we'll look at is ruby

play50:08

so ruby is unlike most other object

play50:12

oriented programming languages it

play50:14

definitely isn't inspired by c

play50:18

java or c sharp

play50:20

instead it has more in common with small

play50:23

talk at least in terms of the overall

play50:26

strategy

play50:28

that

play50:28

ruby uses in terms of its support for

play50:31

object-oriented programming

play50:33

so in ruby everything is an object and

play50:37

in this sense ruby is a much more pure

play50:40

object-oriented programming language

play50:43

than we saw in the case of c plus

play50:46

java and c sharp

play50:49

all computations are handled through

play50:51

message passing so this is again similar

play50:55

to what we saw in small talk

play50:58

now one thing that sets ruby apart is

play51:01

that class definitions are executable so

play51:04

what this means is a secondary

play51:05

definition

play51:06

for the class can be provided and this

play51:09

secondary definition can then add

play51:12

members to the class so what this means

play51:15

is that a class's interface can be

play51:19

different at different times during

play51:22

execution so what i would like you to do

play51:24

at this point is to pause the video and

play51:27

try to think of how this affects the

play51:30

programming language evaluation criteria

play51:32

we've been using through this course

play51:40

now in ruby method definitions are also

play51:43

executable

play51:44

so this means that the definition of a

play51:47

method can also change

play51:50

through the course of runtime

play51:53

again i'd like you to pause the video at

play51:55

this point and try to think of how this

play51:58

affects the programming language

play52:00

evaluation criteria we've been using

play52:07

now similar to what we saw in small talk

play52:10

in ruby all variables are typeless

play52:14

object references and this of course

play52:16

makes sense because everything in ruby

play52:20

is an object and in general we want to

play52:23

perform dynamic binding so therefore we

play52:26

need to use references

play52:31

in ruby access control is different for

play52:34

data and methods within a class so all

play52:38

data within a class is private and this

play52:41

cannot be changed there's no way to

play52:43

create public data

play52:45

or protected data

play52:48

within a class in ruby all data is

play52:51

encapsulated within its class and can

play52:53

only be accessed by methods within that

play52:57

class

play52:58

so what i would like you to do at this

play52:59

point is to pause the video and try to

play53:02

think of how this affects the

play53:04

programming language evaluation criteria

play53:06

we've been using

play53:12

now methods in ruby can be either public

play53:16

private or protected and these work in a

play53:18

similar fashion to what we saw in c

play53:21

plus

play53:23

however method access is checked at run

play53:27

in c

play53:28

plus this access can usually be done at

play53:32

compile time so what i would like you to

play53:34

do at this point is again pause the

play53:36

video and try to explain why method

play53:39

access must be checked at runtime in

play53:42

ruby

play53:48

now getters and setters for data members

play53:52

within ruby classes can be defined by

play53:56

means of shortcuts so we can specify for

play53:59

instance a shortcut for a reader or a

play54:02

writer for a particular member variable

play54:05

and this will then automatically create

play54:08

a gato or a setter method depending on

play54:12

which attribute we use

play54:14

again

play54:15

in the slides

play54:17

in the notes for this particular slide

play54:19

i've included an example of these gator

play54:23

and seta shortcuts

play54:28

now ruby doesn't directly support

play54:30

multiple inheritance as c plus does

play54:34

so in other words a class can only

play54:37

directly inherit from another class

play54:40

now what is interesting in ruby

play54:43

is that if we have a parent class that

play54:45

has a number of members and we have a

play54:48

child class that inherits from the

play54:50

parent class then it is possible for the

play54:53

child class to change the access control

play54:57

of the members inherited from the parent

play55:00

class so for example something that is

play55:02

public in the parent class could be

play55:05

inherited in the child class and given

play55:08

different access control

play55:10

so what this means is that subclasses

play55:12

are not necessarily subtypes in ruby

play55:17

now while ruby doesn't directly support

play55:20

multiple inheritance it can provide some

play55:22

of the features of multiple inheritance

play55:25

by means of what are called modules

play55:29

so a module can have a number of methods

play55:32

or functions defined within it and it

play55:35

can be included in a class in which case

play55:39

it's then referred to as a mix in

play55:41

because we are mixing the module into

play55:44

the class that it is included within so

play55:48

what this means then is that the modules

play55:50

functions are added to the methods of

play55:53

the class they are mixed together

play55:56

essentially and this provides a kind of

play55:59

simulated multiple inheritance

play56:02

now

play56:03

functions

play56:05

when they are called must include the

play56:08

name of the module that the function is

play56:10

defined within so this ensures that

play56:13

there aren't any name collisions as

play56:15

would be the case with true multiple

play56:17

inheritance or for instance a diamond

play56:20

inheritance situation that we discussed

play56:23

in the previous lecture

play56:25

now it's also important to understand

play56:27

that ruby doesn't directly support

play56:30

abstract classes so this notion of a

play56:33

class that cannot be instantiated

play56:36

doesn't exist in ruby

play56:41

now in terms of dynamic message binding

play56:44

ruby uses the same approach as small

play56:48

talk so in ruby all variables are

play56:50

typeless object references and they are

play56:53

polymorphic in nature

play56:56

so what this means is if we call a

play57:00

method on an object then that method

play57:03

will be bound correctly to whatever

play57:07

particular instance the polymorphic

play57:10

variable is actually referring to

play57:14

because all binding of messages is

play57:18

dynamic in nature this means it must

play57:20

happen at runtime so what i would like

play57:22

you to do at this point is to pause the

play57:25

video and try to think of advantages and

play57:29

disadvantages of this kind of approach

play57:37

all right so that then concludes all of

play57:40

the lectures for cars triple three

play57:43

all that remains for me to say is good

play57:46

luck in your preparation for the exam

play57:49

and as i've mentioned before please

play57:52

prepare using both the slides and the

play57:55

textbook

play57:57

and i wish you all the best of luck in

play57:59

those preparations

Rate This

5.0 / 5 (0 votes)

Related Tags
OOP ConceptsC++ InheritanceJava InterfacesC# StructsRuby ModulesPolymorphismAccess ControlDynamic BindingProgramming LanguagesObject Encapsulation