COS 333: Chapter 12, Part 2
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
📘 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.
🔑 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.
🔄 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.
🚧 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++.
🔄 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.
🌐 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.
💠 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.
🎓 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)
💡C++
💡Java
💡C#
💡Ruby
💡Inheritance
💡Polymorphism
💡Access Control
💡Interface
💡Dynamic Message Binding
💡Abstract Class
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
welcome to the final lecture for this
course where we'll be dealing with the
second part of chapter 12 in which we'll
continue looking at object-oriented
programming
in today's lecture we'll continue
looking at examples of object-oriented
programming languages
and we'll discuss each in terms of the
design issues related to object-oriented
programming that we introduced in the
last lecture we'll specifically be
looking at c plus java c sharp and ruby
in this lecture
so we'll begin our discussion by looking
at c plus plus
firstly some general characteristics
related to the programming language so c
plus evolved from two main languages
c and similar 67
as you should know by now c is a purely
imperative programming language with no
object oriented model at all and c
inherits a lot of c's syntax
you should also recall from earlier on
in the course that similar 67 was the
first programming language to introduce
the notion of objects but it didn't have
a full object oriented inheritance model
so c
took this object model from simula 67
and extended it into a fully object
oriented system
now c plus plus is one of the most
widely used object oriented programming
languages today
it's considered a hybrid language for
two reasons firstly it has a mixed type
system so it has a full imperative type
system which is largely inherited from c
so it has things like structs unions
enumerations and so on but then added to
that it also has an object-oriented
class system which allows for full
object-oriented programming and the
two-type systems can work together so
you don't have to choose between using
one or the other
c plus plus also allows functions that
are essentially standalone functions in
other words they are not part of a class
so this allows for a mixture of both
procedural programming and object
oriented programming with classes and
once again these can be mixed in the
same program there's no enforced
separation between the two
now c plus plus primarily was designed
with efficiency in mind so it is a very
efficient
um fast executing object-oriented
programming language particularly if we
compare it to small talk which we
discussed in the previous lecture which
is around 10 times slower than
equivalent c plus code
next we'll look at inheritance in c
plus
so all members within a c
class have access controls so when we're
talking about a member we are talking
about a member variable in other words a
variable that is part of a class we're
also talking about methods within the
class which in c plus are called member
functions of the class
so we have three kinds of access
controls that can be used for class
members in c
public private and protected
so a public member is visible to
sub-classes of the class in which the
member exists as well as client code in
other words code that is using the class
can directly access those members so
typically we would use the public
modifier for anything that is part of
the public interface that client code
should be able to use
in general we wouldn't usually declare
member variables as public because these
should be hidden within the class in
question
next we have private access control so
this means that a member is only visible
within the class it's not visible to
client code and additionally the member
is also visible to any friends of the
class so recall that a class can define
either a function
or an entire other class as a friend and
then the friend function or friend class
will be able to access anything that is
private within the class that has
granted friendship
and then thirdly we have protected
access control here a member is visible
to the class as well as friends but also
then sub classes of the class in which
the member occurs so any derived class
of the class in which we have a
protected member can access that
protected member
however a protected member is not
visible to client code either so
protected
access control then lies somewhere
between public and private
it allows part of the inheritance
hierarchy to still have access to the
protected member however client code is
still cut off from it and generally
speaking the client code then must go
through the public interface of a class
now in c plus inheritance itself can
also have an axis control attached to it
and this will then define potential
changes in the access controls of
inherited members within subclasses
so there are two types that are
discussed in the textbook there is a
third type that the textbook doesn't
discuss
so we have public derivation
also called public inheritance which is
the default kind of inheritance that is
typically used in practice then there's
private derivation or private
inheritance and then thirdly there's
also protected derivation or inheritance
which the textbook doesn't focus on i'll
briefly touch on that in a moment
so with public derivation
the derived class inherits all of the
public and protected members from the
parent class and those public and
protected members do not have their
access controls changed so anything that
is public in the parent class is also
public in the derived class anything
that is protected in the parent class is
also protected in the derived class and
as i said this is the default kind of
inheritance that we typically deal with
now private derivation will inherit the
public and protected members from the
parent class into the derived class so
anything that is public or protected in
the parent class will appear in the
derived class
however what happens then to these
public and protected members is that
they become private in the derived class
so this turns out to disallow subclasses
from being subtypes in other words the
derived class no longer behaves in the
same way as the parent class does
so what i would like you to do is to
pause the video at this point and try to
explain why this is the case why does
private inheritance disallow subclasses
from being subtypes
what i would also like you to do at this
point is to pause the video and try to
think of a situation in which private
derivation would be useful in practice
right so then the third kind of
derivation that i said i'd briefly touch
on is protected derivation and this is
very similar to private's derivation
again the derived class inherits all of
the public and protected members from
the parent class
however instead of changing these
members access controls to private they
are instead changed to protect it so
what i would like you to also do at this
point is to pause the video and try to
think of how protected derivation will
affect subclasses in relation to the
parent classes
here we have a simple program code
example that illustrates the difference
between public and private inheritance
in c plus class
so firstly we have a class called base
class which is defined up at the top
here and this has a number of member
variables so firstly we've got two
private member variables a and x then
we've got two protected member variables
b and y and finally we have two public
member variables
c and z
now we have two classes firstly subclass
one and secondly subclass 2 and both of
these inherit from base class
as you can see
by the inclusion of the name of the base
class after the name of the class being
defined others either subclass 1 or sub
class 2.
now the difference is that in the case
of subclass 1 the inheritance is marked
as public and in subclass 2 the
inheritance is marked as private
so how does inheritance differ then
within subclass 1 and subclass 2
well firstly for both subclass 1 and
subclass 2 everything that is private
within the base class is not provided
within either of those classes
so the only way that subclass 1 or
subclass 2 can access the variables a
and x
is through either the public or the
protected interface of the base class so
in other words they would have to use
either a public or a protected member
function in base class in order to
access
a and x
now subclass 1 inherits everything that
is protected and public so it will
inherit b y
c
and z
and the axis controls for b y c and z
will remain exactly the same in subclass
1.
so b and y will be inherited in subclass
1 and they will both still be protected
in subclass 1 and c and z will be
inherited in subclass 1 and they will
also still be public in subclass 1.
now in subclass 2 as we saw our
inheritance is private so subclass 2
again inherits b y c and z in other
words everything that is protected and
public within the base class however it
changes
the access controls for b y c and c so
that all four of those are now private
within subclass 2. so what this means is
that subclass 2 is no longer
a sub type of base class because it no
longer behaves the same way as base
class does because it's changed the
access controls
for those protected and public member
variables
now generally speaking in practice
we wouldn't use private inheritance in c
plus
as is it's generally not very useful for
us to inherit a number of members from
the parent class and then just simply
make them all private so c plus provides
what is called re-exportation
and basically what this means is that a
member that is private because of
private inheritance or private
derivation
can be re-exported which then makes that
member visible as if it had been
inherited publicly and we use the scope
resolution operator in order to do this
which is represented by two colons so
here's an example of how we would go
about doing this here we have subclass 3
and this is a class that inherits from
our base class from our previous example
and it does so privately so exactly as
we saw in our previous example
all of the public and protected members
in base class are inherited and they
become private within subclass 3
but now what we can do is we can choose
to make the member variable c
public again so c was public in base
class
and then we use this notation where we
specify that we are now using the base
classes version of the c member variable
that re-exports c and makes it public
again in subclass 3.
now note that the notation that i've
used on this slide is a little bit
different to the notation in the
textbook the textbook
books approach does work however it's a
deprecated approach so it's not as
commonly used anymore so please refer to
the code example on this slide when
preparing for this material
now c plus unlike many object-oriented
programming languages does support
multiple inheritance and here we have an
example of simple multiple inheritance
so we have two classes firstly class
thread and secondly class drawing and
both of these are parent classes we then
have a third class called draw thread
and this inherits them from two classes
namely thread and drawing and we have to
specify what kind of inheritance we want
in both cases so the inheritance from
thread is public and the inheritance
from drawing is also public so this
means then that draw thread has two
parents
the class thread and the class drawing
and it inherits all of the public and
protected members from those two classes
now as i mentioned in the previous
lecture there's an issue that arises
with multiple inheritance if the parent
classes have members that have the same
names if in this example
if the thread and drawing classes both
had a member that had exactly the same
name in that case if we were to refer to
that name in draw thread then the
compiler would return an error saying
that it doesn't know which class that
member belongs to so in that case we
need to then explicitly specify which of
the members we are referring to
and again we use the scope resolution
operator in order to do that so we would
have to then specifically in this
example specify thread colon codon and
then the member name or drawing colon
colon and then the member name and that
would be within the draw thread class
now c plus plus also provides the
programmer with maximum flexibility when
it comes to message binding
so it is up to the programmer or
developer of a class at the design time
of that class to decide whether a member
function will use static message binding
or dynamic message binding
now by default a member function that
isn't explicitly stated to use dynamic
message binding uses static message
binding and this means that that member
function will not work through a
polymorphic reference in other words a
polymorphic pointer
now the reason that this is the default
in c plus is that it is much faster than
dynamic binding and as we saw before c
plus was designed for maximum efficiency
so the default is static message binding
and if dynamic message binding is not
required then a programmer will not
specify that dynamic message binding is
required which means then that the
static message binding will be more
efficient
now dynamic message binding needs to be
explicitly specified by the programmer
and they do this by using the special
word virtual
when they define the member function and
what this means then is that the virtual
member function can be called through
polymorphic variables in other words
polymorphic pointers
and the methods then will be dynamically
bound to the messages that call those
methods
so in that case the message binding then
will be dynamic
now c plus plus also provides the
concept of a pure virtual function and
this is an abstract function which we
discussed in the previous lecture and
this is a virtual function with no
definition attached to it so we simply
provide then the signature for this
virtual function and we specify equals
zero with it which indicates that it has
no definition it has no body associated
with it so then a class that has at
least one pure virtual function is
considered an abstract class and as we
discussed in the previous lecture we
cannot instantiate that class
also any derived class if that derived
class does not override and provide an
implementation
for that pure virtual function then that
derived class will also be abstract in
nature
so if we want to concretely instantiate
a derived class we must provide an
implementation of any pure virtual
functions that are defined in the
parents class
the example on this slide illustrates
how message binding works in c plus
so we have a number of classes in this
example first of all we have a class
called shape
and we see that shape has a public
interface and amongst the public member
functions we have a member function
called draw
this
receives no parameters and it also has a
void return type meaning that it returns
nothing
now we've marked this as virtual so this
means that the draw member function
will use dynamic message binding however
we've also specified this equals zero
and that indicates that this is a pure
virtual member function
so we see that there is no body that is
defined for this draw member function
and what this then implies is we cannot
create an object of class shape
also any class that inherits from class
shape
must provide an overrided draw method
and this method is required if we want
to create an object of that class
so here we have a class called circle
and we can see that this publicly
inherits from class
shape
and we've provided in the public section
of this class an overrided version of
the draw method you can see it has
exactly the same signature as the draw
pure virtual function in the shape class
so the function is named draw it
receives no parameters and it has a void
return type however here we do provide
an implementation for it so what this
now means is that the circle class is a
concrete class in other words we can
create an object of class circle
we have a similar thing with class
rectangles a class rectangle publicly
inherits from class shape and it also
provides a draw member function and
again this has an implementation so this
means we can create an object of class
rectangle and finally we have class
square at the bottom over here and
square inherits publicly from the
rectangle class
and we saw that the rectangle class
inherits publicly from the shape class
and once again the square class then
provides a public implementation of the
draw member function if it had not
provided its own implementation for the
draw member function then it would have
inherited the draw implemented for its
parent class in other words the
rectangle class
so now let's look at how we can use
these classes
so the first two variable definitions we
have here are quite simple
here we have a variable called sq and
its type is a square pointer so it is a
pointer or a memory address of a square
object and we assign to that a new
square so that will call the default
constructor of the square class we've
just seen that the square class
does have an implementation for the draw
member function and therefore it is not
an abstract clause so we can create an
object of the square class
similarly we can do the same thing for
the rectangle class so here we have a
variable called rect its type is a
rectangle pointer and we set that to a
new rectangle and again because the
rectangle class actually implements this
draw member function we are allowed to
create an object of class rectangle now
it's important to note that we wouldn't
be able to do the same kind of thing
with the shape class however we can
create a variable that is a shape
pointer which is exactly what we do over
here so we have ptr shape and its type
is a shape point in other words a
pointer to a shape object or a memory
address of a shape object now note that
we haven't initialized this to anything
and we wouldn't be able to assign to
this a new shape because we can't create
an object of class shape however what we
can do is we can assign to our shape
pointer the variable
is q
which we saw
is a pointer to a square
object and this is allowed because we
are using our variable ptr shape
polymorphically
it can have assigned to it an instance
of any of its derived classes so we
could also assign to ptr shape the
variable rate
right so we have the nptr shape and it's
actually pointing to a square object
even though it is a pointed to a shape
now we can call the draw member function
on ptr shape and notice that we use this
arrow notation over here and this is
because ptr shape is a pointer so we
can't use a dot notation we need to use
an arrow notation which means that we
de-reference the pointer and then call
the draw method on that
now because
in the
shape class
which we have seen is the type of the
pointer for ptr shape
we see that that is a virtual member
function this means that this draw
method will then be dynamically bound
the call will be dynamically bound to
the correct implementation of that draw
member function so in other words what
will happen then is that because ptr
shape is pointing to a square object
the draw will then be the draw in class
square which will be the one that will
be called and this happens dynamically
at run time so this is why we are
talking about dynamic message binding
now finally if we look at the wrecked
pointer which we saw up here we have
simply defined as a point to a rectangle
object and we assigned to it a new
rectangle object
if we now call draw on that and again we
need to use this arrow notation because
rect is a pointer
then draw will be statically bound and
this is because the
wrecked
pointer
is actually a pointer to a rectangle so
dynamic message binding doesn't have to
happen at this point it will simply call
the draw member function that has been
defined in the rectangle class itself so
that will then be statically bound and
because static binding is faster than
dynamic binding that means that that
member function call will be a very
quick very efficient call that will take
place
now in the previous lecture we spoke
about the problem of object truncation
or object slicing when we deal with
objects that have been allocated on the
stack and this is definitely a problem
with c
plus
so if objects are allocated from the
stack then assignment to a parent class
variable will truncate
an object of the child class the derived
class and also the message binding is no
longer dynamic it's always static so
let's look at this in terms of this
program code example over here here we
have two variables so we've got sq which
is of type square and we have rect which
is of type rectangle now notice that
neither of these are pointers and they
are not assigned to new objects that
have been created from the heap so in
other words they do not behave
polymorphically the way we saw in the
previous example
instead both sq and rect are now stack
allocated objects
now we are allowed to perform this
assignment so here we are assigning sq
to the variable rect now this is allowed
because as we saw on the previous slides
example the square class inherits from
the rectangle class a square is a kind
of rectangle
however what happens here is a copy of
data members so this isn't a reference
reassignment or a reference copy that
takes place
instead we copy all of the data from the
square object that is relevant for the
raked object and we copy that across so
what this means is if the square class
were to define any additional member
variables that the rectangle class does
not contain then those wouldn't be
copied over in this assignment
now even more important than this is if
we call the draw on our wrecked variable
over here which we know is a stack
allocated variable then this will
statically
call
the draw member function within the
rectangle class
even though what we've actually assigned
here is a square
so this would have obviously have worked
differently if we were working with
polymorphic pointers if we were working
with polymorphic pointers then what
would have happened is that the square
classes draw member function would have
been called but because we're working
with stack allocated object the draw
that is called is the draw
for the type of the object that is that
it is called on in other words it is the
draw implementation for the type
of the wrecked object which is as we've
seen here the class rectangle
next we'll move on to our discussion on
object-oriented programming support
within the java programming language
now as we've mentioned previously in
this course job is fairly closely
related to c plus
a lot of the features that java supports
were inspired by c plus in the first
place
so through this discussion we'll be
focusing on the differences between java
and c
rather than exhaustively rehashing
things that are the same between the two
programming languages
so we saw in c plus that c plus plus has
a full imperative language type model as
well as support for object oriented
programming now java is somewhat
different it's much more object oriented
than c
c x
so all data in java will take the form
of objects except for the primitive
types things like ins floats doubles
booleans and so on
now if these primitive types need to be
handled as objects then java provides
wrapper classes for these primitives
types and these just simply install a
primitive type variable so for example
there is an integer class and this just
simply stores an into value within it
now in c plus plus classes can be
standalone classes they don't
necessarily have to form part of the
same class hierarchy in java this is
different so all objects are part of a
class hierarchy and there is an object
class right at the root of that
hierarchy so every class
is a subtype of object in java
also all objects are heap dynamic in
nature so we have no such concept um as
a stack allocated object in java that
does not exist they're all heap dynamic
and mostly they are allocated using the
new special word
there they're also referenced through
reference variables so they don't use
pointers as is the case in c plus and as
we've discussed previously in this
course references are a much cleaner
solution than pointers they eliminate a
lot of the problems that are associated
with pointers
now memory management in c plus plus
must all be handled manually so in c
plus one must make sure that the class
is completely deallocated using its
e-structure
in java this isn't the case so memory
management in java is handled through a
garbage collector which automatically
cleans up memory when an object is not
required anymore
now a finalized method is implicitly
called when the garbage collector is
about to reclaim the object storage and
it is possible for the programmer to
actually implement this finalized method
and perform specialized operations
within it however in general practice
there isn't any need for a programmer to
implement a finalized method
inheritance in java is also a bit
different to c
so java only supports single inheritance
it doesn't have true multiple
inheritance the way that c plus plus
does
now java does provide the concept of an
interface so an interface is like an
abstract class
in fact it's like an abstract class in
which all of the methods are abstract
so this provides some of the benefits of
multiple inheritance but not the
drawbacks that we saw associated with
for example diamond inheritance which we
spoke about in the previous lecture
so an interface is only allowed to
provide method declarations and named
constants
so here we have an example of an
interface in java this is a public
interface and the interface is called
comparable it's for a generic type t
and we've provided inside this interf
interface then a method declaration for
a method called compare2
that returns an integer value it
receives a variable of type t
and it is public in nature so what this
means now is that anything any class
that implements the comparable interface
must then provide an implementation for
the compare to method and that is
required
and the interface can't provide any
implementation for compare2 or in fact
any other methods within it and it also
doesn't have instance variables so it's
kind of a limited sort of multiple
inheritance it's not true multiple
inheritance because an interface can't
implement all of the things that a class
can it can't have member variables it
can't have implementations for any of
its methods
so the use of interfaces then
sort of simulates a kind of a multiple
inheritance but it's not full multiple
inheritance and it's much simpler than
in c
plus so what i would like you to do at
this point is to pause the video and try
to explain why this use of interfaces is
much simpler in java and what kinds of
problems will the use of interfaces
alleviate in the case of java that we
would see in c plus
now java also allows methods to be
defined as final and a final method
cannot be overridden in a derived class
so final essentially means that any
derived clause
must then refer to the final method
that implementation cannot be changed in
any of the derived classes
binding in java is a lot simpler than in
c plus
so
basically almost all message binding in
java is dynamic in nature so all
messages are dynamically bound to their
methods and static binding only takes
place in very specific circumstances
and these circumstances are only
situations when methods cannot be
overridden so if a method cannot be
overridden then it means that dynamic
binding doesn't make sense in any case
because there can't be an overridden
method that can be bound to so in that
case static binding then makes sense and
java will automatically assume that the
binding will be static in nature so in
practice this happens for final methods
we've just seen that a final method
cannot be overridden in a derived class
also a private method cannot be
overridden because it is private within
the class it is defined in so it doesn't
make sense that it could be overridden
by a derived class and also any static
method in other words a class method
that is defined for an entire class
cannot be overridden in a derived class
so in none of these three cases do we
then have dynamic binding taking place
static binding will take place for these
three cases but in all other cases
dynamic binding is assumed so what i
would like you to do is to pause the
video at this point and try to explain
what effect this will have on the cost
of java in comparison to c plus plus
another interesting way that java
differs from c plus plus is that c plus
doesn't support nested classes whereas
java has a couple of different types of
nested classes
now in general a nested class is only
visible to the nesting class in other
words the class that is containing the
nested class
so in practice a nested class would only
be used in a situation where it has very
limited functionality that's only
applicable to the class within which it
resides
now it is possible to nest one class
inside another within java and i'll call
the nested class in a class and the
nesting class the outer class so the
outer class contains the inner class
so the nesting class in other words the
outer class can access all of the
members in the nested in other words the
inner class so anything even
anything that is defined as private
within the nested the inner class will
be accessible to the outer class
now there are two kinds of nested
classes that can be defined within
java if they are listed inside a class
firstly we have non-static nested
classes which are also referred to as
inner classes
and these can access all of the members
of the nested classes nesting class so
in other words the inner class can
access all of the members in the outer
class
now a static nested class is fairly
similar the only difference is that it
can't access the members of the nesting
class in other words the inner class
cannot access the members of the outer
class
now it is also possible for nested
classes to be anonymous i won't go into
too much detail here on anonymous nested
classes but essentially an anonymous
nested class doesn't have a name
associated with it we simply specify
that it is a class
which is assigned to a variable and it
is
inheriting from a specific class
implementing a specific interface
and we then provide some limited
functionality for it and anonymous
classes are intended just as single use
classes so we will only be creating one
object of that class not multiple
objects
now in the notes for the slide i provide
some example code of anonymous nested
classes so please refer to that when
preparing for the exam
now in java it's also possible to nest
classes inside methods within a nesting
class so this is even more local than
a class nested inside of another class
this is a nesting inside an actual
method so these are referred to in java
as local nested classes these classes
don't have class access specifiers so we
don't specify that they're either
private or public this is because
private and public don't make sense in
this context because the nested class is
only visible within the nesting method
so the nesting method can then access
all of the members within this nested
class and we would typically then use
these local nested classes in a
situation where the class only needs to
be used by one specific method and not
even the rest of the outer nesting class
next we'll move on to c sharp so support
for object oriented programming in c
sharp is very similar to java and this
is because java was one of the big
influencing languages of c sharp
now what is interesting in c sharp is
that c sharp provides both classes and
structs java does not have the notion of
a struct
now classes in c sharp are very similar
to classes in java however structs are
different
now in c plus a struct and a class are
almost exactly the same thing
it is possible for example to inherit
one struct from another it's also
possible to have polymorphic pointers
two structs and dynamic message binding
works with structs the only difference
between structs and classes in c plus
plus is the default access modifier for
members of structs and classes
respectively
now in c sharp a struct is a very
different thing to a class and struct is
a lot less powerful than a struct in c
plus plus
so a struct is strictly a stack dynamic
construct structs can only be allocated
from the stack
they cannot be dynamically allocated
from the heap as they could be in c
plus
also there's no inheritance involved in
c-sharp structs so it isn't possible to
define a struct that inherits from
another struct in the c sharp
and finally and probably most
importantly
no members within a c-sharp struct can
reference the struct itself so for
example it's not possible to create a
node struct in c sharp
where the node contains a reference to
another node
creating therefore a linked list type of
structure if you wanted to create that
kind of linked structure or something
like a tree you cannot use structs in c
sharp you must use classes
so what i would like you to do at this
point is to pause the video and
considering the differences between
structs and classes in c sharp
what do you think structs are intended
for in terms of their use and their
purpose in c-sharp programs
c sharp uses c plus like syntax for
defining classes where inheritance is
involved
now it is of course possible in c sharp
if we have a parent class that has a
method defined within it to have a
derived child class of that parent class
and the child class can then override
the method that is provided in the
parent class however c sharp is a little
bit more explicit than c plus is in this
regard so the overriding method its
definition must be marked with the
special word new
so what i would like you to do at this
point is to pause the video and try to
think of an advantage
associated with this approach of
explicitly marking every overwritten
methods definition with new
and also can you think of any
disadvantages
now it is possible to call the parent
classes version of an overridden class
explicitly
and there you use the base prefix so for
example if we wanted to explicitly
access the draw method in the parent
class and not in the current class then
we would use base in order to indicate
that we are referring to the parent
class now c-sharp also supports
interfaces and these work in the same
way as we explained in java earlier on
in this lecture
dynamic message binding in c sharp again
works in a fairly similar fashion to c
plus however once again c sharp is a bit
more explicit about this than c plus
plus is
so if we have dynamic binding of
messages to methods then as in c plus we
need to in c sharp uh define the base
class method and mark it as virtual so
this is the same as in c plus
however when we implement a virtual
method in a derived class in other words
a child class then that implemented
virtual method must be marked with the
special word override so this is not the
case in c plus so what i would like you
to do at this point is to pause the
video and try to think of an advantage
of this approach in c sharp and also can
you think of any disadvantages
now abstract methods in c sharp are
marked as abstract so they don't use the
pure virtual function notation that we
saw in c plus
and then also unlike c plus plus all
classes in c sharp are derived from a
root object class so in this sense c
sharp is more similar to java which also
has a whole class hierarchy with a
single root class
in c plus we've seen that every class is
independent and doesn't necessarily have
to be part of a single class hierarchy
like java c-sharp also supports nested
classes however support for these nested
classes in c sharp is more limited than
what we saw in java
so a class can be directly nested within
an outer nesting class
and this nested class the inner class
behaves like a static nested class in
java so what this means is the outer
class can access any of the members
within the inner class however the inner
class cannot access any of the members
of the outer class that contains the
nested class
there's no
nested class structure in c sharp that
is similar to the non-static nested
classes in java so there's no way for
this in a nested class to have access to
the members of the outer
containing class
the last object-oriented programming
language that we'll look at is ruby
so ruby is unlike most other object
oriented programming languages it
definitely isn't inspired by c
java or c sharp
instead it has more in common with small
talk at least in terms of the overall
strategy
that
ruby uses in terms of its support for
object-oriented programming
so in ruby everything is an object and
in this sense ruby is a much more pure
object-oriented programming language
than we saw in the case of c plus
java and c sharp
all computations are handled through
message passing so this is again similar
to what we saw in small talk
now one thing that sets ruby apart is
that class definitions are executable so
what this means is a secondary
definition
for the class can be provided and this
secondary definition can then add
members to the class so what this means
is that a class's interface can be
different at different times during
execution so what i would like you to do
at this point is to pause the video and
try to think of how this affects the
programming language evaluation criteria
we've been using through this course
now in ruby method definitions are also
executable
so this means that the definition of a
method can also change
through the course of runtime
again i'd like you to pause the video at
this point and try to think of how this
affects the programming language
evaluation criteria we've been using
now similar to what we saw in small talk
in ruby all variables are typeless
object references and this of course
makes sense because everything in ruby
is an object and in general we want to
perform dynamic binding so therefore we
need to use references
in ruby access control is different for
data and methods within a class so all
data within a class is private and this
cannot be changed there's no way to
create public data
or protected data
within a class in ruby all data is
encapsulated within its class and can
only be accessed by methods within that
class
so what i would like you to do at this
point is to pause the video and try to
think of how this affects the
programming language evaluation criteria
we've been using
now methods in ruby can be either public
private or protected and these work in a
similar fashion to what we saw in c
plus
however method access is checked at run
in c
plus this access can usually be done at
compile time so what i would like you to
do at this point is again pause the
video and try to explain why method
access must be checked at runtime in
ruby
now getters and setters for data members
within ruby classes can be defined by
means of shortcuts so we can specify for
instance a shortcut for a reader or a
writer for a particular member variable
and this will then automatically create
a gato or a setter method depending on
which attribute we use
again
in the slides
in the notes for this particular slide
i've included an example of these gator
and seta shortcuts
now ruby doesn't directly support
multiple inheritance as c plus does
so in other words a class can only
directly inherit from another class
now what is interesting in ruby
is that if we have a parent class that
has a number of members and we have a
child class that inherits from the
parent class then it is possible for the
child class to change the access control
of the members inherited from the parent
class so for example something that is
public in the parent class could be
inherited in the child class and given
different access control
so what this means is that subclasses
are not necessarily subtypes in ruby
now while ruby doesn't directly support
multiple inheritance it can provide some
of the features of multiple inheritance
by means of what are called modules
so a module can have a number of methods
or functions defined within it and it
can be included in a class in which case
it's then referred to as a mix in
because we are mixing the module into
the class that it is included within so
what this means then is that the modules
functions are added to the methods of
the class they are mixed together
essentially and this provides a kind of
simulated multiple inheritance
now
functions
when they are called must include the
name of the module that the function is
defined within so this ensures that
there aren't any name collisions as
would be the case with true multiple
inheritance or for instance a diamond
inheritance situation that we discussed
in the previous lecture
now it's also important to understand
that ruby doesn't directly support
abstract classes so this notion of a
class that cannot be instantiated
doesn't exist in ruby
now in terms of dynamic message binding
ruby uses the same approach as small
talk so in ruby all variables are
typeless object references and they are
polymorphic in nature
so what this means is if we call a
method on an object then that method
will be bound correctly to whatever
particular instance the polymorphic
variable is actually referring to
because all binding of messages is
dynamic in nature this means it must
happen at runtime so what i would like
you to do at this point is to pause the
video and try to think of advantages and
disadvantages of this kind of approach
all right so that then concludes all of
the lectures for cars triple three
all that remains for me to say is good
luck in your preparation for the exam
and as i've mentioned before please
prepare using both the slides and the
textbook
and i wish you all the best of luck in
those preparations
Browse More Related Video
![](https://i.ytimg.com/vi/5VXqrjH7PbQ/hqdefault.jpg?sqp=-oaymwExCJADEOABSFryq4qpAyMIARUAAIhCGAHwAQH4Af4EgALgA4oCDAgAEAEYZSBJKEMwDw==&rs=AOn4CLAEds4PbOd_Oq33k3v9rd1xN1mv9Q)
COS 333: Chapter 12, Part 1
![](https://i.ytimg.com/vi/5Y74odV3IAI/hq720.jpg)
Features of Object Oriented Programming Part 2 | C ++ Tutorial | Mr. Kishore
![](https://i.ytimg.com/vi/9Mg4YpsGSkg/hqdefault.jpg?sqp=-oaymwEXCJADEOABSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLCvZP76xNzXIQesAKAnIBQo1Qynrw)
COS 333: Chapter 2, Part 3
![](https://i.ytimg.com/vi/u99wAoBjDvQ/hqdefault.jpg?sqp=-oaymwEXCJADEOABSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLBWxeLOUOHCnY8dcEgkvVrFQTJy9w)
OOPS Interview Questions and Answers | Object Oriented Programming Interview Questions C#
![](https://i.ytimg.com/vi/_cbfR690u74/hq720.jpg)
Object Oriented Programming Features Part 3 | C ++ Tutorial | Mr. Kishore
![](https://i.ytimg.com/vi/AcCfCEbpIBQ/hq720.jpg?sqp=-oaymwEmCIAKENAF8quKqQMa8AEB-AH-BIAC4AOKAgwIABABGGUgSShDMA8=&rs=AOn4CLBnPzahWyt0lTOXe7HlGlvzGGFDag)
COS 333: Chapter 11, Part 2
5.0 / 5 (0 votes)