1 / 60

OOP

OOP. Structure of programming languages. Programming Paradigm. A way of conceptualizing what it means to perform computation and how tasks to be carried out on the computer should be structured and organized. Imperative : Machine-model based

alodie
Download Presentation

OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOP Structure of programming languages

  2. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried out on the computer should be structured and organized. • Imperative : Machine-model based • Functional : Equations; Expression Evaluation • Logical : First-order Logic Deduction • Object-Oriented : Programming with Data Types

  3. Information hiding • We have no idea HOW an object is stored, nor do we care. All we care about is the behavior of the data according to the defined functions. • Information hiding can be built into any language • We will look at mechanisms later to enforce information hiding (Smalltalk, C++, Java, Ada). We will call this enforcement encapsulation

  4. object is a collection of operations that share state. The object exists at run-time. • A class is a textual description of the state variables (fields) and the operations (methods). • A module is a syntactic mechanism for grouping related elements, and forms the basis for enforcing information hiding.

  5. Introducing objects and classes into the Language • Class definition (via Inheritance) • class variables (** not supported but can be easily incorporated **) • instance variables (state) • assignments (state changes) • method definitions • method invocations • initialization • Object creation (instantiation)

  6. Additional Syntax (define the-grammar ’( (program ((arbno class-decl) expression)a-program) . . . (class-decl ("class" identifier "extends" identifier (arbno "field" identifier) (arbno method-decl) )a-class-decl) (method-decl ("method" identifier "(" (separated-list identifier ",") ")" expression)a-method-decl) (expression ("new" identifier "(" (separated-list expression ",") ")")new-object-exp) (expression ("send" expression identifier "(" (separated-list expression ",") ")")method-app-exp) (expression ("super" identifier "(" (separated-list expression ",") ")")super-call-exp) ) ) )

  7. Storage for C++ classes • Visibility of objects: • public: globally known • private: locally known only • protected -- provides for inheritance

  8. Scope • Fields of class are have class scope: accessible to any class member • fields accessed by all class methods • Parameters of method and any variables declared within body of method have local scope:accessible only to that method • not to any other part of the code • In general, scope of a variable is block of code within which it is declared • block of code is defined by braces { }

  9. Consider the following program: public class ParamTest1 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } }

  10. Consider the following program: public class ParamTest1 { public static void main (String[] args) { int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } } What's the flow of control?

  11. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } } What's the flow of control?

  12. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } } What's the flow of control?

  13. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } } What's the flow of control?

  14. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); x = x * x; System.out.println("method1: x is now " + x); } } What's the flow of control?

  15. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; System.out.println("method1: x is now " + x); } } What's the flow of control?

  16. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } What's the flow of control?

  17. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } What's the flow of control?

  18. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } main: number is 4 What's printed?

  19. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } main: number is 4 method1: x is 4 What's printed?

  20. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } main: number is 4 method1: x is 4 method1: x is now 16 What's printed?

  21. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } main: number is 4 method1: x is 4 method1: x is now 16 ????????????????????? What's printed?

  22. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } main: number is 4 method1: x is 4 method1: x is now 16 main: number is now 4 What's printed?

  23. public class ParamTest1 { public static void main (String[] args) { 1 int number = 4; 2 System.out.println("main: number is " + number); 3 method1(number); 7 System.out.println("main: number is now " + number); } public static void method1(int x) { 4 System.out.println("method1: x is " + x); 5 x = x * x; 6 System.out.println("method1: x is now " + x); } } main: number is 4 method1: x is 4 method1: x is now 16 main: number is now 4 Why not 16?

  24. Static variables declared within class associated with class, not instance Instance variables declared within class associated with instance accessible throughout object, lifetime of object Local variables declared within method accessible throughout method, lifetime of method Parameters declared in parameter list of method accessible throughout method, lifetime of method Variable Types

  25. Static Fields/Methods • Static fields belong to whole class • nonstatic fields belong to instantiated object • Static methods can only use static fields • nonstatic methods can use either nonstatic or static fields object: Giraffe2 class: Giraffe neckLength numGiraffes object: Giraffe1 neckLength sayHowTall() getGiraffeCount() sayHowTall()

  26. Static Variables public class Giraffe { private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; } public void sayHowTall() { System.out.println(“Neck is “ + neckLength); } } • how would we keep track of how many giraffes we’ve made? • need a way to declare variable that "belongs" to class definition itself • as opposed to variable included with every instance (object) of the class

  27. Static Variables public class Giraffe { private static int numGiraffes; private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; } public void sayHowTall() { System.out.println(“Neck is “ + neckLength); } } • static variable: variable shared among all instances of class • aka class variable • use "static" as modifier in variable declaration • Only one copy of static variable for all objects of class

  28. Static Variables public class Giraffe { private static int numGiraffes; private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; numGiraffes++; } public void sayHowTall() { System.out.println(“Neck is “ + neckLength); } } • updating static variable is straightforward • increment in constructor

  29. Static Methods • Static method "belongs" to the class itself • not to objects that are instances of class • aka class method • Do not have to instantiate object of class in order to invoke static method of that class • Can use class name instead of object name to invoke static method • compiler will give error if static method attempts to use nonstatic variable • Therefore, the main method can access only static or local variables.

  30. Static Methods public class Giraffe { private static int numGiraffes; private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; numGiraffes++; } public void sayHowTall() { System.out.println("Neck is " + neckLength); } public static int getGiraffeCount() { return numGiraffes; } } • static method example

  31. Calling Static Method Example public class UseGiraffes { public static void main (String[] args) { System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); Giraffe fred = new Giraffe(200); Giraffe bobby = new Giraffe(220); Giraffe ethel = new Giraffe(190); Giraffe hortense = new Giraffe(250); System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); } } • Note that Giraffe is class name, not object name! • at first line haven’t created any Giraffe objects yet

  32. Static Methods public class UseGiraffes { public static void main (String[] args) { System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); Giraffe fred = new Giraffe(200); Giraffe bobby = new Giraffe(220); Giraffe ethel = new Giraffe(190); Giraffe hortense = new Giraffe(250); System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); } } • Now you know what all these words mean • main method can access only static or local variables

  33. Inheritance • Inheritance provides for passing information from one data object to another automatically • It provides a form of data scope similar to syntactic scope. • Inheritance through data in object oriented languages is explicitthrough derived types.

  34. Power of inheritance class rational { public: mult( ...) { ... } protected: error( ...) { ... } ... private: ... } class complex:rational { public: mult( ...) { ... } private: ... } complex X;

  35. Power of inheritance • Function error is passed (inherited) to class complex, so X.error is a valid function call. Any derived class can invoke error and a legal function will be executed. • But what if we want error to print out the type of its argument? (i.e., want to know if error occurred in a rational or complex data?)

  36. Method Overriding • If child class defines method with same name and signature as method in parent class • say child's version overrides parent's version in favor of its own • reminder: signature is number, type, and order of parameters • Writing our own toString() method for class overrides existing, inherited toString() method • Where was it inherited from?

  37. Method Overriding • Where was it inherited from? • All classes that aren't explicitly extended from a named class are by default extended from Object class • Object class includes a toString() method • so... class header public class myClass • is actually same as public class myClass extends Object

  38. Overriding Variables • You can, but you shouldn't • Possible for child class to declare variable with same name as variable inherited from parent class • one in child class is called shadow variable • confuses everyone! • Child class already can gain access to inherited variable with same name • there's no good reason to declare new variable with the same name

  39. Virtual functions • Base class: • class rational { • error() { cout << name() << endl; } • string name() { return “Rational”;} ... } • Derived class: • class complex: rational { • string name() { return “Complex”;} ... } • But if error is called, Rationalis always printed since the call rational::name is compiled into class rational for the call in the error function.

  40. Virtual functions • But if name is defined as: • virtual string name() { return “Rational”;} • then name() is defined as a virtualfunction and the function name in the current object is invoked when name() is called in rational::error.

  41. Implementing virtual functions • Virtual functions imply a runtime descriptor with a location of object • rational A; • complex B; • A.error()  error will call name() in rational • B.error()  error will call name() in complex

  42. Mixin inheritance • Assume want to add feature X to both class A and B: • Usual way is to redefine both classes.

  43. Mixin inheritance • Mixin inheritance: Have definition which is addition to base class (Not part of C++) • For example, the following is possible syntax: • featureX mixin {int valcounter} Add field to object • newclassA class A mod featureX; • newclassB class B mod featureX; • Can get similar effect with multiple inheritance: • class newclassA:A,featureX { ... } • class newclassB:B,featureX { ... }

  44. Abstract classes • A class C might be abstract • No instance of C can be created. • But instances of subclasses of C can be created. • Useful to capture the commonality shared by a set of classes. Expression Var. binary Value

  45. Abstract clases • abstract class Expression { … } • class Binary extends Expression {…} • class Variable extends Expression { … } • class Value extends Expression { … } • In an abstract class • Some of the methods are defined inside the abstract class • Rest of the methods defined via the subclasses

  46. Class Interfaces • Typically, identifies a collection of methods to be implemented by various classes. • All methods are “abstract” : not defined in the interface. • Concrete methods are implemented in the classes implementing the interface. • All methods must be implemented in such class definitions. • Can write code that works on anything that fulfills contract • even classes that don’t exist yet!

  47. Class Interfaces • Public abstract interface Enumeration { • // the method signatures appear here • public abstract boolean hasMoreElements(); • public abstract object nextElement (); • } • Public class stringTok extends Object implements Enumeration{ • // the method implementations appear here • public boolean hasMoreElements() {…} • public Object nextElement() {…} • }

  48. How to cater for Polymorphism • Polymorphism = poly (many) + morph (form) • Polymorphismis the ability of a data object to that can take on or assume many different forms. • Polymorphism can be categorized into 2 types • Ad-hoc Polymorphism • Universal Polymorphism • Parametric (discussed with Functional Programming) • Inclusion (discussed later in the lecture)

  49. Polymorphism Ad-Hoc Universal Coercion Overloading Parametric Inclusion How to cater for Polymorphism Ad-Hoc polymorphism is obtained when a function works, or appears to work on several different types (which may not exhibit a common structure) and may behave in unrelated ways for each type. Universal polymorphismis obtained when a function works uniformly on a range of types; these types normally exhibit some common structure.

  50. Polymorphism • A polymorphic subroutine is one that can accept arguments of different types for the same parameter • max(x,y){ max = x>y?x:y }could be reused for any type for which > is well-defined • A polymorphic variable(parameter) is one that can refer to objects of multiple types. ML: x : ‘a • True (or “pure”) polymorphism always implies code reuse: the same code is used for arguments of different types.

More Related