20) Object Oriented Programming =============================== * Object oriented programming != objected oriented language. class Node: def __init__(self): self.data = "" self.link = 0 class Stack: def __init__(self): self.top = 0 def add(stack, data): n = Node() n.data = data n.link = stack.top stack.top = n def hasMore(stack): return stack.top != 0 def remove(stack): n = stack.top stack.top = n.link return n.data s = Stack() add(s, "AA") add(s, "BB") add(s, "CC") while hasMore(s): print remove(s) 1) What are the problems with this implementation? 1.2) Can you come up with a better one? class Node: def __init__(self, data, link): self.data = data self.link = link class Stack: def __init__(self): self.top = 0 def add(self, element): n = Node(element, self.top) self.top = n def hasMore(self): return self.top != 0 def remove(self): aux = self.top self.top = aux.link return aux.data s = Stack() s.add("AA") s.add(2) s.add("BB") s.add([1, 2, 3]) while s.hasMore(): print str(s.remove()) * A new paradigm: the data knows how to do things. The data is intelligent. 2) If the data knows how to do things on itself, do I need its internal representation? 2.1) How to find the size of an array in C? 2.2) How to find the size of an array in Java? 3) Can anyone name the first language called 'OO'? - records + high-order-functions + self referencing 4) What is indeed OO programming? 5) Many languages are called OO. Can you name a few? - Java, Scala, C#, C++, Eiffel and also - Ruby, Smalltalk, Python, JavaScript, Perl and even also - Occaml 6.1) What is the difference between these two groups of languages? 7) Which features are common to languages in the first group? - Static typing - Classes: - Modules - Types - Computation as message passing - Information hiding - Exception handling - Inheritance - The open-closed principle - Single inheritance - Multiple inheritance (name clashes, repeated inheritance) - Overloading - Subtyping - Liskov's substitution principle - Dynamic binding - Genericity - Constrained - Run-time type interrogation - Heavy heap usage - Garbage collection * Static typing is the ability to discover every type in a program during compilation time. 8) What is the fundamental compilation unit in C? 8.1) What about Java? * A class is the implementation of an abstract data type. 9) What is a class good for? 10) What is an object? * An object is a run-time instance of a class. * An object has a location in memory, has a state, needs to be created, might be destroyed. * A class is only text. - defines a compilation unit, - is a module, - is also a type. 11) How does an object talks to another one? * A runtime object oriented system is like a limbo, full of black boxes called objects, which are linked through unidirectional channels called methods. 12) Why information hidding is important? 12.1) How do I get info hiding in Java? 12.2) How do I get info hiding in C? 13) What does exception handling has to do with OO? 13.1) How does it improves reuse? * Inheritance is a mechanism of reuse. It supports the open-closed principle: "A module should be open for extension and should be closed in terms of information hiding." 14) What kinds of extensions I am talking about? 14.1) Does java has multiple-inheritance? 14.2) Why not? * the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C? 15) What is overloading? 15.1) Examples of overloading in C? 15.2) What overloading has to do with inheritance? 16) Has anyone heard of subtyping? 16.1) What subtyping has to do with inheritance? * The Liskov substitution principle: if S is a subtype of T, then S can be used in any situation where T is expected. 17) Can anyone give me an example? * Dynamic binding: the target of a call is chose at run-time. 18) How java knows which method to call for each object? public class Zoo { class Animal { public void eat() { System.out.println(this + " is eating"); } public String toString () { return "Animal"; } } class Mammal extends Animal { public void suckMilk() { System.out.println(this + " is sucking"); } public String toString () { return "Mammal"; } } class Dog extends Mammal { public void bark() { System.out.println(this + " is barking"); } public String toString () { return "Dog"; } } public void test () { Animal a1 = new Animal(); Animal a2 = new Mammal(); Animal a3 = new Dog(); // // What will be printed in each case below? // System.out.println(a1); System.out.println(a2); System.out.println(a3); a1.eat(); // a2.suckMilk(); a2.eat(); // Dog d1 = a3; Dog d1 = new Dog(); Mammal m1 = d1; d1.bark(); m1.suckMilk(); d1.suckMilk(); // Type casts: Dog d2 = (Dog)a3; // a3.bark(); d2.bark(); // Dog d3 = (Dog)a2; } public static void main(String a[]) { Zoo z = new Zoo(); z.test(); } } 19) What is parametry polymorphism? 19.1) What is reused with parametric polymorphism? 19.2) How is this implemented in Java? * Generics can be constrained: - public void drawAll(List shapes) 20) How I would know the type of a data structure in C? 20.1) How to do this in Java? * reflection is the process by which a computer program can observe and modify its own structure and behavior. // With reflection Class cls = Class.forName("Foo"); Object foo = cls.newInstance(); Method method = cls.getMethod("hello", null); method.invoke(foo, null); 21) How is memory organized at run-time? 21.1) Which of these two parts is more used? 22) Which problems can happen in the heap management? 22.1) How to handle them? * Data X Functions => top down X bottom up 23) How is the design of a compiler? Top down: - translate source to machine code ... 1 Parse file 2 Optimize program 3 Generate machine code. ... 1.1 Open file 1.2 Read tokens 1.3 Produce AST 1.4 close files 24) Where does data come in in the top-down approach? Bottom up: We need: "ParseTree", "Optimizer", "CodeGenerator", "Parser", "Lexer", "CodeGenerator", "ArchDescription", "File", "Token". 25) What are the advantages of top-down? - Structured and sistematic - Global view - Easier to explain 26) What are the advantages of bottom up? - Reusability - Data independence - No temporal ordering - Easier to change * In practice, programming is a mix of these two styles, but OO languages favour bottom-up because we can work with interfaces, instead of needing implementations. 27) How should be a productive development environment? - Refactoring - Fast update - Persistence - Browsing - Documentation