Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Object-Oriented Concepts – towards implementation CS3340.

Similar presentations


Presentation on theme: "Basic Object-Oriented Concepts – towards implementation CS3340."— Presentation transcript:

1 Basic Object-Oriented Concepts – towards implementation CS3340

2 The goals of OOP Objectives:modular programming.modular programming. reusable code/modules. inheritance. can define interfacesinterfaces 2

3 OOP Abstraction Encapsulation Information Hiding Polymorphism Hierarchy 3 Class Object Inheritance Interface ConceptualImplementation

4 Procedural (like C) vs. Object-Oriented Programming -- HISTORY The unit in procedural programming is function, and unit in object-oriented programming is class Procedural programming concentrates on creating functions, while object-oriented programming starts from isolating the classes, and then look for the methods inside them. Procedural programming separates the data of the program from the operations that manipulate the data, while object-oriented programming focus on both of them figure1: proceduralfigure2: object-oriented

5 Going conception  implementation defining terms first 5

6 Some Terms OOP = object oriented Programming Class = a template representing a self-contained element of a computer program (i.e. a module) that represents related information and has the capability to accomplish specific tasks through the invocation of methods (operations). 6

7 An class has behaviors In old style programming, you had: o data, which was completely passive o functions, which could manipulate any data An class contains both data (elements) and operations (methods) that manipulate that data o An object is active, not passive; it does things o An object is responsible for its own data But: it can expose that data to other objects 7

8 Class: data and operations/methods An object contains both data and methods that manipulate that data o The data represent the state of the object o Data can also describe the relationships between this object and other objects Example: A CheckingAccount might have o A balance (the internal state of the account) o An owner (some object representing a person) Example: A Dog might have o A breed o A name o An owner 8

9 Object: An “instance” of a class An instance will have a state representing values of the data. Example: The Dogs in the image –each are different. 9 Instance is another word for object…they are synonyms

10 Example: A “Rabbit” class You could (in a game, for example) create a class representing a rabbit It would have data: o How hungry it is o How frightened it is o Where it is And methods: o eat, hide, run, dig Now create an instance/object of our Rabbit class to represent BugsBunny 10

11 Wait – are you confued? Class and Object “Class” refers to a blueprint. It defines the variables and methods the objects support “Object” is an instance of a class. Each object has a class which defines its data and behavior

12 Concept: Classes are like Abstract Data Types An Abstract Data Type (ADT) bundles together: o some data, representing an object or "thing" o the operations on that data The operations defined by the ADT are the only operations permitted on its data Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc. Classes enforce this bundling together o If all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data 12 This slide relates Class to OOP concepts

13 Special note: in the following slides I will discuss points and show you EARLY some java –don’t worry if you don’t understand now ---you will learn java in this class 13

14 Example of a class 14 class Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter } Don’t worry I will teach you java

15 Objects must be created int n; does two things: o It declares that n is an integer variable o It allocates space to hold a value for n o For a primitive, this is all that is needed Employee secretary; also does two things o It declares that secretary is type Employee o It allocates space to hold a reference to an Employee o For an object, this is not all that is needed secretary = new Employee ( ); o This allocate space to hold a value for the Employee o Until you do this, the Employee is null 15 Don’t worry I will teach you java

16 Class Members A class can have three kinds of members: o fields/data/variables : data variables which determine the status of the class or an object o methods : executable code of the class built from statements. It allows us to manipulate/change the status of an object or access the value of the data member o nested classes and nested interfaces We will learn later about nested classes— basically is a class defined in a class

17 Inheritance a mechanism that enables one class to inherit all the behaviors and attributes of another class. subclass = a class that inherits from another class. superclass = a class that is its inheritance to another class. 17

18 More on inheritance Note: Subclasses can override methods they have inherited if they want to change the corresponding behavior. For example, the method to calculate the Expected Lifespan for each Class of Dog, Cat, and Horse may be different from their superclass Four- Legged Animal. 18

19 Inheritance: Classes form a hierarchy Classes are arranged in a treelike structure called a hierarchy Every class may have one or more subclasses 19

20 Another Example of a hierarchy—this one dealing with windows 20 A FileDialog is a Dialog is a Window is a Container Container PanelScrollPane Window DialogFrame FileDialog

21 Example of inheritance 21 class Person { String name; int age; void birthday () { age = age + 1; } class Employee extends Person { double salary; void pay () {...} } Every Employee has name and age fields and birthday method as well as a salary field and a pay method. Don’t worry I will teach you java

22 How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does both But the secretary is still "blank" ( null ) secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message 22 Don’t worry I will teach you java

23 How to reference a field or method Inside a class, no dots are necessary class Person {... age = age + 1;...} Outside a class, you need to say which object you are talking to if (john.age < 75) john.birthday (); If you don't have an object, you cannot use its fields or methods! 23 Don’t worry I will teach you java

24 Concept: this object – identifying one’s self Inside a class, no dots are necessary, because o you are working on this object If you wish, you can make it explicit: class Person {... this.age = this.age + 1;...} this is like an extra parameter to the method You usually don't need to use this 24 Don’t worry I will teach you java

25 Concept: Methods can be overridden – a form of polymorphism So birds can fly. Except penguins. 25 class Bird extends Animal { void fly (String destination) { location = destination; } class Penguin extends Bird { void fly (String whatever) { } } Don’t worry I will teach you java

26 Interacting with Ojbects  send messages Bird someBird = pingu; someBird.fly ("South America"); Did pingu actually go anywhere? o You sent the message fly(...) to pingu o If pingu is a penguin, he ignored it o Otherwise he used the method defined in Bird You did not directly call any method o You cannot tell, without studying the program, which method actually gets used o The same statement may result in different methods being used at different times 26 Don’t worry I will teach you java

27 Some things to think about …we will revisit 27

28 Advice: Restrict access Always, always strive for a narrow interface Follow the principle of information hiding: o the caller should know as little as possible about how the method does its job o the method should know little or nothing about where or why it is being called Make as much as possible private Your class is responsible for it’s own data; don’t allow other classes to screw it up! 28

29 Advice: Use setters and getters This way the object maintains control Setters and getters have conventional names: set DataName, get DataName, is DataName (booleans only) 29 class Employee extends Person { private double salary; private boolean male; public void setSalary (double newSalary) { salary = newSalary; } public double getSalary () { return salary; } public boolean isMale() { return male; } }

30 Kinds of access Java provides four levels of access: o public : available everywhere o protected : available within the package (in the same subdirectory) and to all subclasses o [default]: available within the package o private : only available within the class itself The default is called package visibility In small programs this isn't important...right? 30

31 Now… 31

32 From Budd Book on OOP 1. Everything is an Object (in pure OOP) 2. Computation is done via Objects Communicating with each other by requesting objects to perform actions. At some point at least a few object need to perform some work besides passing on requests to other objects/agents. 3. Each Object has its own memory (and in pure OOP consists of other objects) 4. Every object is an instance of a class. 5. A Class is a repository for behavior associated with an object and the kind of information stored in an instances memory. 6. Classes are organized into a single rooted tree structure. Memory and behavior of an instance of a class is automatically available (inheritance) to any class associated with a descendant. 32


Download ppt "Basic Object-Oriented Concepts – towards implementation CS3340."

Similar presentations


Ads by Google