0% found this document useful (0 votes)
5 views27 pages

Oo 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Oo 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

CS571: Programming Languages

OO Languages I
(a.k.a. Under the Hood of Java/C++)

CS571 Programming Languages 1


Inheritance

2
Inheritance
Allows code reuse. If you plan to write some code
for “GradStudent” and know your friend has
written the code for “Student”, why not just use
your friend’s code as a base and modify based on
it, rather than rewrite from scratch?
Inheritance in essence is a “code + code = a
bigger piece of code”-kind of operation. It is
related to subtyping, but these two concepts are
distinct, and many OO languages actually treat
them orthogonally.

3
Inheritance (Cont.)
A class B can inherit some or all of the fields and
methods of another class A by declaring that
class in its definition.
 B is called a subclass of A and A a superclass of

B.
 Java: public class B extends A

 C++: class B: public/private/protected A

superclass
Transportation

subclass subclass
car train

4
Inheritance (Cont.)

Properties
 Inheritance is transitive: inherits from all
ancestors.

5
Two Uses of Inheritance (I)

Extension.
 A subclass can add data members and
member functions of its own.

6
Two Uses of Inheritance (II)
Redefinition (method overriding): refers to the
fact that an implementation of a method in a
subclass supersedes the implementation of the
same method in the base class
 A derived class is more specific than a base

class.
public class A
{ public void f1() {System.out.println(“A::f1");}}
public class B extends A
{ public void f1() {System.out.println("B::f1");}}
B r = new B();
r.f1();
Output: B::f1 7
Inheritance Example: Node

Node
Data
setData
getData

ListNode TreeNode
Next Left
Right
setNext
getNext setLeft
setRight
getLeft
getRight

8
Example: Node (Java)
public class Node {
private int data;
public Node(int newData)
{ setData(newData);}
public void setData(int newData)
{ data = newData;}
public int getData()
{ return data;}
}

9
Example: ListNode (Java)
public class ListNode extends Node {
private ListNode next;
public ListNode(int newData) {
//super: access methods of the parent class, including those that might
be overridden by members of the current class and constructors.
super(newData);
setNext(null);
}
public void setNext(ListNode newNext)
{ next = newNext;}
public ListNode getNext()
{ return next;}
}
10
Example: TreeNode (Java)
pubic class TreeNode extends Node {
private TreeNode left;
private TreeNode right;
public TreeNode(int newData) { // Constructor
super(newData);
setLeft(null);
setRight(null); }
public void setLeft(TreeNode newLeft)
{ left = newLeft; }
public void setRight(TreeNode newRight)
{ right = newRight; }
public TreeNode getLeft()
{ return left; }
public TreeNode getRight()
{ return right; }
}
11
Inheritance (C++)
class B : public A
 All public/protected members of A remain
public/protected in B. Private members of A
are unavailable in B

class B : private A
 B has private access to the public and
protected members of A.
class B: protected A
 The public and protected parts of the base
class become protected in the derived class.

12
Inheritance Example (C++)

Point
x class Point{
y
protected: int x, y;
public:
Circle 3D-Point void set (int a, int b);
x x };
y y
r z

class Circle : public class 3D-Point: public


Point{ Point{
private: double r; private: int z;
}; };

13
Single Inheritance
Single inheritance: each subclass inherits from
only one superclass
 All the examples you have seen so far are
single inheritance.
 The only mode for Java

14
Multiple Inheritance
Multiple inheritance: a class may inherit from
two or more superclasses
 C++ provides
acyclic graph
A
Difficult to resolve
references to methods.
B C What happens if B, but not
C, redefines a method of
D A?
Java has multiple interface inheritance which is
almost as powerful, and much easier to
implement.
Multiple inheritance is out of scope of this class
15
Subtyping

16
“Is-a” Property
Possibilities:
 A cat is a pet. (subtyping relationship)
 My Alex is a cat. (class-object relationship)
In fact, even the latter CAN be modeled as a subtyping
relationship. Many new-generation type systems can
address this: singleton types -- but this is out of the
scope of our classes.
Informally, think of subtyping as set containment (this is
the informal “programmer” view; type theorists may
disagree.)

17
Subtype Principle
In any operation that expects an object of type T, it
is acceptable to supply object of type T’, which is
“subtype” of T. T is a “supertype” of T’.
So what it means is that it is always OK to assign a
variable of the subtype to a variable of the
supertype, without making your program to crash
at run time.
It just so happens that Java inheritance hierarchy
satisfies the same relation, but this is more of a
“coincidence” than a necessity.
 Many OO programming languages do not fit into this
“coincidence”, including C++.
 Interested students should read “Inheritance is Not
Subtyping” (POPL 1989)
18
Inheritance and Subtyping
public class A
{ public void f1() {System.out.println("f1");}
}
public class B extends A
{ public void f2() {System.out.println("f2");}
} A

B inherits from A. Inheritance graph: B

All of the functions that apply to A can also apply


to B.
B r = new B();
r.f1();
Output: f1
19
Subtype Principle

Example (Java):
public class A
{ public void f1() {System.out.println("f1");}
}
public class B extends A
{ public void f2() {System.out.println("f2");}
}

20
Subtype Principle (Cont’d)
Example (Java):
public class Main {
public static void main(String args[]){
A i;
B s;
s = new B();
i = s;
s.f1();
i.f1();
s.f2();
i.f2(); }
}

21
Subtype Principle (Cont’d)
Example (Java):
public class Main {
public static void main(String args[]){
A i;
B s;
s = new B();
i = s;
s.f1(); //legal
i.f1();
s.f2();
i.f2(); }
}

22
Subtype Principle (Cont’d)
Example (Java):
public class Main {
public static void main(String args[]){
A i;
B s;
s = new B();
i = s;
s.f1(); //legal
i.f1(); //legal
s.f2();
i.f2(); }
}

23
Subtype Principle (Cont’d)
Example (Java):
public class Main {
public static void main(String args[]){
A i;
B s;
s = new B();
i = s;
s.f1(); //legal
i.f1(); //legal
s.f2(); //legal
i.f2(); }
}

24
Subtype Principle (Cont’d)
Example (Java):
public class Main {
public static void main(String args[]){
A i;
B s;
s = new B();
i = s;
s.f1(); //legal
i.f1(); //legal
s.f2(); //legal
i.f2(); } //illegal
}

25
C++: Inheritance != Subtyping
class A
{ public: void f();
…};
class B: private A
{…}; // no public redefinition of f

A* x = new A();
B* y = new B();
x->f();
y->f();
Subtype principle: In any operation that expects an
object of type T, it is acceptable to supply object of
type T’, which is subtype of T.
26
C++: Inheritance != Subtyping
class A
{ public: void f();
…};
class B: private A
{…}; // no public redefinition of f

A* x = new A();
B* y – new B ();
x->f(); //legal
y->f(); //illegal – inheritance != subtyping

In Java, all subclassed objects have subtypes. In


C++, this may not be true.

27

You might also like