Advanced Programming Language Concepts

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Advanced Programming Language

Concepts
CT006-3-3-APLC and Version VC1

Language Comparisons: Classes


Topic and Structure of the Lesson

1. Classes
2. Inheritance
3. Single Inheritance
4. Multiple Inheritances
5. Generic classes

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 2 of 29


Learning Outcomes

At the end of this chapter, you should be


able to
• Critically evaluate the languages in terms
of classes concept

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 3 of 29


Key Terms
1. Class
2. Object
3. Instances
4. Inheritance
5. Polymorphic type

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 4 of 29


CLASSES

• An object is a group of variable components equipped with a group


of operations that access these variables.

• A class is a template for a set of similar objects. All these objects


would have the same variable components and be equipped with
the same operations. The variable components are private and the
operations are public.

• A constructor method is an operation that creates (and usually


initialises) a new object (called an instance) of the class. It must be
public.

• An instance method is an operation that inspects and/or updates


one or more of the variables (attributes) of an existing object of the
class. It is normally public.

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 5 of 29


INHERITANCE

A subclass of a class is a set of objects which are similar to each


other but richer than the superclass.

A subclass inherits all the attributes and methods from the


superclass. The subclass may have further attributes and methods
in order to achieve specialisation. It may also override some of
the superclass methods, using the same method names, with
functionally new versions.

The use of classes, especially with inheritance (see later), are


supposed to support re-usability, i.e. the reuse of classes and their
methods, and the developer time, maintenance and testing. These
ideas are supported by object-oriented languages, such as Java
and C++, but also, somewhat differently, by Ada 95 and Haskell.

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 6 of 29


SINGLE INHERITANCE in Java

abstract class Shape


{
protected double x, y;
// this shape’s centre
// available to subclasses only
public double distance( )
{ // inherited by subclasses
return Math.sqrt(x*x + y*y);
}

public final void move (double dx, double dy)


{ // cannot be overridden
x = x + dx;
y = y + dy;
}

public abstract void draw();


// must be overridden
}

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 7 of 29


It would be artificial to have any actual shape as a superclass for all
other shapes. The idea of a shape is indeed abstract. So no draw
method can be defined.

Abstract classes can never have a constructor methods.

However, all shapes will have a centre, and all centres can be
moved, but we don’t allow subclasses to have their own move
methods (hence the word, final).

All actual shapes, i.e. subclasses, may have further methods and
attributes, to achieve specialisation.

Attributes, x and y, are available to all subclasses but not to any


uninherited classes (hence the word, protected).

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 8 of 29


CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 9 of 29
class Point extends Shape
{ // inherit from Shape
public Point ( )
{
x = 0.0; // constructor
y = 0.0; // initialises
}

public void draw ()


{
// details of draw
// must be defined
}
}

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 10 of 29


class Circle extends Shape
{
private double r; // extra attribute

public Circle (double radius)


{
x = 0.0;
y = 0.0;
r = radius;
}

public void draw ()


{
// details of draw
}

public double getDiam ()


{
return 2.0 * r;
}
}

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 11 of 29


class Box extends Shape
{
private double w, h;
public Box (double width, double height)
{
x = 0.0;
y = 0.0;
w = width;
h = height;
}
public void draw ( )
{
// details of draw
}
public double getWidth ( )
{
return w;
}
public double getHeight ( )
{
return h;
}
}
CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 12 of 29
class Square extends Box
{
// all methods inherited

public Square (double side)


{
// use superclass constructor
super (side, side);
}
}

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 13 of 29


MULTIPLE INHERITANCE in C++

Class hierarchy for


Animal abstract
Animals
weight
height

Mammal
Flier abstract Bird abstract
abstract
wingSpan eggSize
gestationTime

Cat Bat Eagle Penguin


sonarRange

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 14 of 29


MULTIPLE INHERITANCE in C++

class Animal
{
private:
float weight;
float height;
public:
// some methods
}

class Mammal: public Animal


{ // inherit everything from Animal
private:
float gestationTime;
public:
// extra methods
}

class Bird: public Animal


{ // inherit everything from Animal
private:
float eggSize;
public:
// extra methods
}
CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 15 of 29
class Flyer: public Animal
{ // inherit everything from Animal
private:
float wingSpan;
public:
// extra methods
}
// single inheritance form Mammal
class Cat: public Mammal
{ // inherit everything from Mammal
public:
// extra methods
}
// multiple inheritance from Mammal and Flyer
class Bat: public Mammal, public Flyer
{ // inherit everything from both
private:
float sonarRange;
public:
// extra methods
}
// similarly for subclass Eagle

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 16 of 29


Multiple inheritance does have some difficulties.

1) Conceptual difficulties include confusion regarding the nature of


superclasses – should they be mutually exclusive? For the
example above, should flyers be a superclass? Is this a confusion
of an action, such as flying, with an object, such as a bird?

2) Programming difficulties include inheritance of methods from


both classes. When a method of the same name exists in both
superclasses, which version should be used in the subclass?
Should the programmer specify it? C++ doesn’t allow this
scenario, this implies the programmer cannot always use multiple
inheritance involving someone else’s superclasses.

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 17 of 29


MULTIPLE INHERITANCE (in C++ ?)

Class hierarchy for


Animal abstract
Animals
Method1( )

Mammal Flier
Bird abstract
Method1( ) Method1( )
eggSize
overridden overridden

Bat
Cat Eagle Penguin
Method1( )
inherited

Which version of Method1( ) is inherited? From Mammal or Flier?

NOT permitted in C++

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 18 of 29


Multiple inheritance is also available in Haskell.

Polymorphic type, a, inherits from both classes Ord and Show:

vSort :: (Ord a, Show a) => [a] -> String

Function, vSort, given a list of any type, sorts the list giving the result as a
string (for printing). Class Ord is required for ordering any type (using < ) and
class Show is required for converting any type into a string (using show function).

vSort list = concat[ show x ++ “\n” | x <- sort list]

thus, for example:

vSort [33, 66, 22, 11]


gives
“11\n22\n33\n66”
and
putStr (vSort [(“fred”, 34), (“nick”, 33), (“al”, 44)]
gives
(“al”,44)
(“fred”,34)
(“nick”,33)
CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 19 of 29
GENERIC CLASSES in Java

Parametric polymorphism is not available in OO


languages. However, the same effect can be achieved
using generic classes in C#, C++ and Java (since 2004).

Let us consider examples in Java: defining a


polymorphic Stack class:

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 20 of 29


In “traditional” Java using the artificial umbrella class,
Object, as a polymorphic type:

class Stack
{ // a Stack has elements that are objects
private Object [ ] elements;
private int depth;

public Stack (int size) // constructor


{
depth = 0;
elements = new Object[size];
}
public void push(Object x)
{
elements[depth++] = x;
}
public Object pop( )
{
return elements[---depth];
}
} // end class Stack

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 21 of 29


In an application, for a stack with a mixture of different kinds of
elements (as objects), we could write:

Stack st;
String s, t;
Circle c, d;
....
st = new Stack(100);
s = “Fred”;
c = new Circle(10.0);
....
st.push(s);
st.push(c);
....
t = (String)st.pop(); // casting
d = (Circle)st.pop(); // required

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 22 of 29


Note that return types (of type Object) have to be casted as the actual type
required by the assignment (eventhough the type of the variable is known).

Furthermore, if the Stack had elements all the same type,


e.g. String, then the casting is still required:

st = new Stack(50);
s = “Fred”;
st.push(s);
....
t = (String)st.pop(); // casting

Also, note that the elements must be objects and cannot be primitive (or
basic) types, such as int, float, etc)! To include such types, they would have to
be constructed and used as object types, such as Integer, Float (which are
classes rather than types!

This distinction between object and primitive types does not exist in
Haskell – nor in C#.

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 23 of 29


In “modern” Java (2004) using a generic class definition for Stack as a
polymorphic type:

class Stack <Item>


{ // a Stack has items that are generic
private Item[ ] elements;
private int depth;

public Stack(int size)// constructor


{
depth = 0;
elements = new Item[size];
}
public void push(Item x)
{
elements[depth++) = x;
}
public Item pop( )
{
return elements[--depth];
}
} // end class Stack
CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 24 of 29
In an application, Stack items must be all the same type,
e.g. String, thus casting is NOT required:

Stack <String> st1;


Stack <Circle> st2;
Circle c, d;
String s, t;
....
st1 = new Stack <String>(200);
st2 = new Stack <Circle>(200);
....
s = “Fred”;
st1.push(s);
c = new Circle(1.5);
st2.push(c);
....
t = st1.pop(); // NO casting
d = st2.pop(); // NO casting

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 25 of 29


Quick Review

• What are classes?


• Compare between classes and generic
classes.
• Define parametric polymorphism.

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 26 of 29


Summary of Main Teaching Points

• Classes
• Inheritance
• Single Inheritance
• Multiple Inheritances
• Generic classes

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 27 of 29


Q&A

Any Questions?

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 28 of 29


Next Session

• Imperative Programming - Comparative


study with other paradigm
– Language Comparisons – Side effects
• Simple exercises

CT006-3-3-Advanced Programming Language Concepts Language Comparison: Classes Slide 29 of 29

You might also like