1 / 69

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

brinda
Télécharger la présentation

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. Imperative vs Non-Imperative • Functional/Logic programs specify WHAT is to be computed abstractly, leaving the details of data organization and instruction sequencing to the interpreter. • In contrast, Imperative programs describe the details of HOW the results are to be obtained, in terms of the underlying machine model.

  4. 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

  5. Information hiding -- C example: • typedef struct {i:int; ... } TypeA; • typedef struct { ... } TypeB; • P1 (TypeA X, other data) { ... } - P1:other data  TypeA • P2 (TypeB U, TypeA V) { ... } - P2:TypeA  TypeB

  6. Encapsulated data types--Example: • StudentRecord is type • Externally visible: • void SetName(StudentRecord, Name) • name GetName(StudentRecord) • Internal to module: • char Name[20]; • float GPA; • char Address[50]; • CourseType Schedule[10];

  7. Packages in ADA • package RationalNumber is • type rational is record -- User defined type • num, den: integer • end record; • procedure mult(x in rational; -- Abstract operation • y in rational; z out rational); • end package;

  8. Packages in ADA • package body RationalNumber is -- Encapsulation • procedure mult(x in rational; • y in rational; z out rational) • begin • z.num := x.num * y.num; • z.den := x.den * y.den; • end; • end package;

  9. 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.

  10. 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)

  11. 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) ) ) )

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

  13. 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 { }

  14. Parameter Passing 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); } }

  15. Parameter Passing 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?

  16. Parameter Passing Consider the following program: 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?

  17. Parameter Passing Consider the following program: 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?

  18. Parameter Passing Consider the following program: 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?

  19. Parameter Passing Consider the following program: 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?

  20. Parameter Passing Consider the following program: 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?

  21. Parameter Passing Consider the following program: 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?

  22. Parameter Passing Consider the following program: 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?

  23. Parameter Passing Consider the following program: 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 printed?

  24. Parameter Passing Consider the following program: 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?

  25. Parameter Passing Consider the following program: 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?

  26. Parameter Passing Consider the following program: 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?

  27. Parameter Passing Consider the following program: 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?

  28. Parameter Passing Consider the following program: 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?

  29. Parameter Passing Consider the following program: 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?

  30. 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

  31. 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()

  32. 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

  33. 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

  34. 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

  35. Static Variables • Static variable shared among all instances of class • Only one copy of static variable for all objects of class • Thus changing value of static variable in one object changes it for all others objects too! • Memory space for a static variable established first time containing class is referenced in program

  36. 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.

  37. 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

  38. 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

  39. 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

  40. 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 explicit through derived types.

  41. C++ derived classes class complex: rational { public: void mult( complex x; complex y); { realpt.mult(x.realpt,y.realpt)-realpt.mult(x.imagpt,y.imagpt) ... void initial(complex x) {x.realpt.num = 0; x.realpt.den = 1 }

  42. C++ derived classes // complex inheritsrationalcomponents. private: rational realpt; rational imagpt } . . . complex M, N, P; M.mult(N,P)

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

  44. 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?)

  45. 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?

  46. 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

  47. 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

  48. 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.

  49. 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.

  50. 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

More Related