Introduction to Object Oriented Programming =========================================== 1) Is the program below object oriented? class Node { public String data; public Node link; } class Stack { public Node top; } public class Controller { public static void add(Stack s, String data) { Node n = new Node(); n.data = data; n.link = s.top; s.top = n; } public static boolean hasMore(Stack s) { return s.top != null; } public static String remove(Stack s) { Node n = s.top; s.top = n.link; return n.data; } } public class Main { public static void main(String args[]) { Stack s = new Stack(); Controller.add(s, "AA"); Controller.add(s, "BB"); Controller.add(s, "CC"); while (Controller.hasMore(s)) { String out = Controller.remove(s); System.out.println(out); } } } 1.1) Why? 1.2) What is not good about the program above? 2) How could the design of the program above be improved? public class Stack { private class Node { E data; Node link; public Node(E d, Node l) { data = d; link = l; } } private Node top; public void add(E e) { Node n = new Node(e, this.top); this.top = n; } public boolean hasMore() { return this.top != null; } public E remove() { Node aux = this.top; this.top = aux.link; return aux.data; } public static void main(String args[]) { Stack s = new Stack(); s.add("AA"); s.add("BB"); s.add("CC"); while (s.hasMore()) { System.out.println(s.remove()); } } } * A new paradigm: the data knows how to do things. The data is intelligent. 3) If the data knows how to do things on itself, do I need its internal representation? 3.1) How to find the size of a string in C? 3.2) How to find the size of a String in Java? 4) Can anyone name the first language called 'OO'? - records + high-order-functions + self referencing 5) What is indeed OO programming? 6) 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 7.1) What is the difference between these two groups of languages? 8) Which features are common to languages in the first group? - Two worlds: - Prototype based: Self, JavaScript, ActionScript, Lua - Class based: Java, C#, C++, Python, Ruby, Smalltalk - 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 9) What is the fundamental compilation unit in C? 9.1) What about Java? * A class is the implementation of an abstract data type. 10) What is a class good for? 11) 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. 12) 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. 13) Why information hidding is important? 13.1) How do I get info hiding in Java? 13.2) How do I get info hiding in C? 14) What does exception handling has to do with OO? 14.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." 15) What kinds of extensions I am talking about? 15.1) Does java has multiple-inheritance? 15.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? 16) What is overloading? 16.1) Examples of overloading in C? 16.2) What does overloading have to do with inheritance? 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"; } } 17) Has anyone heard of subtyping? 17.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. 18) Can anyone give me an example? * Dynamic binding: the target of a call is chosen at run-time. 19) What will be printed in each case? public static void main(String a[]) { Animal a1 = new Animal(); Animal a2 = new Mammal(); Animal a3 = new Dog(); 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(); Dog d2 = (Dog)a3; // a3.bark(); d2.bark(); // Dog d3 = (Dog)a2; } 20) What is parametric polymorphism? 20.1) What is reused with parametric polymorphism? 20.2) How is this implemented in Java? import java.util.LinkedList; class Animal { private int weight; public Animal() { weight = 100; } public Animal(int w) { weight = w; } public double price() { return weight * 18.0; } public String toString () { return "Animal"; } } class Bull extends Animal { public Bull(int w) { super(w); } public double price() { return super.price() * 2.71; } public void grass() { System.out.println(this + " is eating grass."); } public String toString () { return "Bull"; } } class Zebu extends Bull { public Zebu(int w) { super(w); } public double price() { return super.price() * 3.14; } public String toString () { return "Zebu"; } } public class Farm { public static void main(String args[]) { // Build the farm: LinkedList farmList = new LinkedList(); farmList.add(new Animal()); farmList.add(new Animal(90)); farmList.add(new Bull(110)); farmList.add(new Zebu(130)); // Compute the whole value of the farm: double price = 0.0; for (Animal a : farmList) { price += a.price(); } System.out.println("The farm is worth R$ " + price); } 21) How to move all the Bulls in the list farmList in the program above to a list of Bulls? 21.1) How I would know the type of a data structure in C? 21.2) How to do this in Java? // Get the grassing animals: LinkedList cattle = new LinkedList(); for (Animal a : farmList) { if (a instanceof Bull) { cattle.add((Bull)a); } } * reflection is the process by which a computer program can observe and modify its own structure and behavior. 22) How to print all the methods declared in a class? public class Reflection { public static void main(String[] args) { try { Class cls = Class.forName("lesson6.Animal"); // Get only the declared methods: Method methods[] = cls.getDeclaredMethods(); for (Method m : methods) { System.out.println(m); } // Create a new instance of the class: Animal a = (Animal) cls.newInstance(); // Invoke price over the new instance: Method m = cls.getMethod("toString", null); System.out.println("Invoking a method on " + m.invoke(a, null)); } catch (Exception e) { e.printStackTrace(); } } } 23) How is memory organized at run-time? 23.1) Which of these two parts is more used? 24) Which problems can happen in the heap management? 24.1) How to handle them? * Modular Programming 25) What properties characterize a modular design? - Decomposition - Composition - Closed meaning - Continuity 26) What is decomposition? - [Design] The capacity to separate a task into subtasks, which can be worked upon independently. - [Software] The capacity to work on each module of the software independently on the others. 26.1) Can you give me counter-examples? - Global variables. - Common initialization routines. 27) What is composition? - Capacity to freely combine different software elements. 27.1) Can you give me good examples? - Unix shell pipes. - Collection libraries. 27.2) Any counter-example? - Programs that rely on macros: it is not always possible to use macros inside other routines, due to variable capture, for instance. 28) What is closed meaning? - Capacity to be understood by a reader who does not have access to other parts of the system. 28.1) Counter-examples? - Sequential dependencies: C depends on B, that depends on A. It is hard to understand C without understanding B, or even A. 29) And continuity: what is this? 29.1) Has anyone heard of continous function? - Small change in specification triggers change in one, or very few modules. 29.2) Good examples? - Programming with constants. 29.3) Counter-examples? - God objects: systems that contain many classes depending on just one. * Data X Functions => top down X bottom up 30) 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 31) Where does data come in the top-down approach? Bottom up: We need: "ParseTree", "Optimizer", "CodeGenerator", "Parser", "Lexer", "CodeGenerator", "ArchDescription", "File", "Token". 32) What are the advantages of top-down? - Structured and sistematic - Global view - Easier to explain 33) 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. 34) How should be a productive development environment? - Refactoring - Fast update - Persistence - Browsing - Documentation