OOP vs. Languages: Exploring Classes, Inheritance, Encapsulation, Polymorphism, Slides of Advanced Computer Programming

An in-depth exploration of the concepts of object-oriented programming (oop) and object-oriented languages (ool). It covers the definitions, observations, and features of oop and ool, with a focus on classes, inheritance, encapsulation, and polymorphism. The document also includes examples of java code and comparisons between oop in ml and non-oop in java.

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani 🇮🇳

4.5

(2)

83 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Orientation
Chapter Sixteen Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download OOP vs. Languages: Exploring Classes, Inheritance, Encapsulation, Polymorphism and more Slides Advanced Computer Programming in PDF only on Docsity!

Object Orientation

Chapter Sixteen Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Definitions

 Give definitions for the following:

– Object-oriented language

– Object-oriented programming

 Then again, why bother?

Chapter Sixteen Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Outline

 16.2 Object-oriented programming

– OO in ML

– Non-OO in Java

 16.3 Object-oriented language features

– Classes

– Prototypes

– Inheritance

– Encapsulation

– Polymorphism

Chapter Sixteen Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Chapter Sixteen Modern Programming Languages, 2nd ed. 5

public class Node { private String data; private Node link; public Node(String theData, Node theLink) { data = theData; link = theLink; } public String getData() { return data; } public Node getLink() { return link; } }

A previous Java example: a node

used to build a stack of strings

Docsity.com

Chapter Sixteen Modern Programming Languages, 2nd ed. 7

datatype message = GetData | GetLink;

datatype response = Data of string | Object of message -> response;

fun node data link GetData = Data data | node data link GetLink = Object link;

Same OO idea in ML.

We have a type for messages and a type for responses.

To construct a node we call node, passing the first two

parameters.

Result is a function of type message->response.

Docsity.com

Node Examples

 Objects responding to messages

 null has to be something of the object type

(message->response); we could use

Chapter Sixteen Modern Programming Languages, 2nd ed. 8

  • val n1 = node "Hello" null; val n1 = fn : message -> response
  • val n2 = node "world" n1; val n2 = fn : message -> response
  • n1 GetData; val it = Data "Hello" : response
  • n2 GetData; val it = Data "world" : response

fun null _ = Data "null";

Docsity.com

Chapter Sixteen Modern Programming Languages, 2nd ed. 10

datatype message = IsNull | Add of string | HasMore | Remove | GetData | GetLink;

datatype response = Pred of bool | Data of string | Removed of (message -> response) * string | Object of message -> response;

fun root _ = Pred false;

Expanded vocabulary of

messages and responses,

for both node and

stack

Root class handles all

messages by returning

Pred false

Docsity.com

Chapter Sixteen Modern Programming Languages, 2nd ed. 11

fun null IsNull = Pred true | null message = root message;

fun node data link GetData = Data data | node data link GetLink = Object link | node _ _ message = root message;

fun stack top HasMore = let val Pred(p) = top IsNull in Pred(not p) end | stack top (Add data) = Object(stack (node data top)) | stack top Remove = let val Object(next) = top GetLink val Data(data) = top GetData in Removed(stack next, data) end | stack _ message = root message;

Docsity.com

Inheritance, Sort Of

 Here is a peekableStack like the one in

Java from Chapter Fifteen:

 This style is rather like a Smalltalk system

– Message passing

– Messages not statically typed

– Unhandled messages passed back to superclass

Chapter Sixteen Modern Programming Languages, 2nd ed. 13

fun peekableStack top Peek = top GetData | peekableStack top message = stack top message;

Docsity.com

Thoughts

 Obviously, not a good way to use ML

– Messages and responses not properly typed

– No compile-time checking of whether a given

object can handle a given message

 (Objective CAML is a dialect that integrates

OO features into ML)

 The point is: it’s possible

 OO programming is not the same as

programming in an OO language

Chapter Sixteen Modern Programming Languages, 2nd ed. (^) Docsity.com 14

Java

 Java is better than ML at supporting an

object-oriented style of programming

 But using Java is no guarantee of object-

orientation

– Can use static methods

– Can put all code in one big class

– Can use classes as records—public fields and

no methods, like C structures

Chapter Sixteen Modern Programming Languages, 2nd ed. (^) Docsity.com 16

Classes Used As Records

Chapter Sixteen Modern Programming Languages, 2nd ed. 17

public class Node { public String data; // Each node has a String... public Node link; // ...and a link to the next Node }

public class Stack{ public Node top; // The top node in the stack }

Docsity.com

Polymorphism

 In Chapter Fifteen: Worklist interface

implemented by Stack, Queue, etc.

 There is a common trick to support this kind

of thing in non-OO solutions

 Each record starts with an element of an

enumeration, identifying what kind of

Worklist it is…

Chapter Sixteen Modern Programming Languages, 2nd ed. (^) Docsity.com 19

A Non-OO Worklist

Chapter Sixteen Modern Programming Languages, 2nd ed. 20

public class Worklist { public static final int STACK = 0; public static final int QUEUE = 1; public static final int PRIORITYQUEUE = 2; public int type; // one of the above Worklist types public Node front; // front Node in the list public Node rear; // unused when type==STACK public int length; // unused when type==STACK }

The type field says what kind of Worklist it is.

Meanings of other fields depend on type.

Methods that manipulate Worklist records must branch on

type…

Docsity.com