1 / 32

Basic Object-Oriented Concepts – towards implementation

Basic Object-Oriented Concepts – towards implementation. CS3340. The goals of OOP. Objectives: modular programming. reusable code/modules. inheritance. can define  interfaces. OOP. Conceptual. Implementation. Abstraction Encapsulation Information Hiding Polymorphism Hierarchy.

jhesson
Download Presentation

Basic Object-Oriented Concepts – towards implementation

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. Basic Object-Oriented Concepts – towards implementation CS3340

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

  3. OOP Conceptual Implementation • Abstraction • Encapsulation • Information Hiding • Polymorphism • Hierarchy • Class • Object • Inheritance • Interface

  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: procedural figure2: object-oriented

  5. Going conception  implementationdefining terms first

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

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

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

  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. Instance is another wordfor 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: • How hungry it is • How frightened it is • Where it is • And methods: • eat, hide, run, dig • Now create an instance/object of our Rabbit class to representBugsBunny

  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: • some data, representing an object or "thing" • 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 • 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 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

  14. Example of a class Don’t worry I will teach you java 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 }

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

  16. Class Members • A class can have three kinds of members: • fields/data/variables: data variables which determine the status of the class or an object • 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 • 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.

  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. 

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

  20. Another Example of a hierarchy—this one dealing with windows Container Panel ScrollPane Window Dialog Frame FileDialog A FileDialog is a Dialog is a Window is a Container

  21. Example of inheritance Don’t worry I will teach you java 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.

  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 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! Don’t worry I will teach you java

  24. Concept: thisobject – identifying one’s self • Inside a class, no dots are necessary, because • 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 Don’t worry I will teach you java

  25. Concept: Methods can be overridden – a form of polymorphism class Bird extends Animal { void fly (String destination) { location = destination; } } • So birds can fly. Except penguins. 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? • You sent the message fly(...) to pingu • If pingu is a penguin, he ignored it • Otherwise he used the method defined in Bird • You did not directly call any method • You cannot tell, without studying the program, which method actually gets used • The same statement may result in different methods being used at different times Don’t worry I will teach you java

  27. Some things to think about …we will revisit

  28. Advice: Restrict access • Always, always strive for a narrow interface • Follow the principle of information hiding: • the caller should know as little as possible about how the method does its job • 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!

  29. Advice: Use setters and getters • This way the object maintains control • Setters and getters have conventional names: setDataName, getDataName, isDataName (booleans only) 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: • public: available everywhere • protected: available within the package (in the same subdirectory) and to all subclasses • [default]: available within the package • private: only available within the class itself • The default is called package visibility • In small programs this isn't important...right?

  31. Now…

  32. From Budd Book on OOP

More Related