1 / 42

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts. Good Questions. What are Objects ? What are Classes ? What are Messages ? What is Inheritance ?. What Are Objects?. Software objects model read-world objects or abstract concepts dog, bicycle, queue Real-world objects have states and behaviors

robyn
Download Presentation

Object-Oriented Programming Concepts

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. Object-Oriented Programming Concepts

  2. Good Questions • What are Objects? • What are Classes? • What are Messages? • What is Inheritance?

  3. What Are Objects? • Software objects model read-world objects or abstract concepts • dog, bicycle, queue • Real-world objects have states and behaviors • Dogs' states: name, color, breed, hungry • Dogs' behaviors: barking fetching • How do Software objects implement real-world objects? • Use variables to implement states • Use methods to implement behaviors • An object is a software bundle of variables and related methods

  4. Visual Representation of A Software Object Variable Method

  5. Software Bicycle • Instance Variables and Instance Method

  6. Encapsulation • The objects' variables make up the center of the object. • Methods surround and hide the object's center from other objects. • Benefit of encapsulation • Modularity • Information hiding • For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.

  7. What Are Classes? • A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind. • An object is an instance of a certain class. • After you have created a class, you must create an instance of it before you can use. • Class variables and class methods • The benefit of Classes: Reusability

  8. Visual Representation of A Software Class

  9. What Are Messages? • Software objects interact and communicate with each other by sending messages to each other.

  10. More on Messages • Components of a Message • The object to whom the message is addressed (Your Bicycle) • The name of the method to perform(changeGears) • Any parameters needed by the method (low gear) • The Benefits of Messages • Messages passing supports all possible interactions between objects (aside from direct variable access) • Objects don’t need to be in the same process or even on the same machine to send and receive messages.

  11. What is Inheritance? • Inheritance allows classes to be defined in terms of other classes • superclass and subclass • Each subclass inherits variables and methods from its superclass. • Subclasses can add variables and methods to the ones they inherit from the superclass. • Subclasses can also override inherited method and provide specialized implementations for those methods. • Inheritance or class hierarchy

  12. Benefits of Inheritance • Programmers can reuse the code in the superclass many times. • Programmers can implement superclasses called abstract classes • Abstract class defines “generic” behaviors • Define and may partially implement the behavior but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

  13. Message Example

  14. Inheritance Example

  15. Object-Oriented Development in Java

  16. Agenda • Java Class and Object Declaration • The Life Cycle of an Object • Controlling Access to Members of a Class

  17. Java Classes and Objects Class: templates for specifying the state and behavior of an object at runtime Object: instances of a class The concepts of class/object provide a mechanism for encapsulation

  18. Class Declaration Variable Instance Variable Class Variable Constructor Method Instance Method Class Method Cleanup Rectangle2.java Basic Structures of a Class

  19. Point and Rectangle Rectangle2.java

  20. The Class Declaration

  21. Constructor • A method in a class that initialize an instance of an object before it's used. • The same name as the class and have no return type • Multiple Constructors: the name name but a different number of arguments or different typed arguments • Method Overloading • Java Provides default constructors. • The special variable, this, can be used inside a method to refer to the object instance. • Rectangle.java

  22. Member Variables Declaration

  23. Local variable is defined inside a block of code or a method. Example (FirstClass.java) Instance Variable: firstVariable Local Variable: half public int getHalf() { int half; // local variable half = firstVariable / 2; return half;} Instance and Local Variable

  24. Methods

  25. Method Declaration

  26. Return a Value from a Method • Use return operator in the method to return the value. • Methods can return a primitive type or a reference type. • The class of the returned object must be either a subclass of or the exact class of the return type.

  27. Method Overload • Signature of a Method: return value, name, parameter list • Method Overloading: Use the same method name with different arguments to group together related methods. • Constructors can also be overloaded. • this(parameters) : Call another constructor within a constructor • Example: FirstClass.java, SecondClass.java, Rectangle.java

  28. Passing Information into a Method • Argument types • primitive and reference data type: Yes • method: No • Argument Names • Can have the same name as one of the class's member variable • Use this to refer to the member variable • Primitive arguments are passed by value. • Reference arguments are passed by reference. • FirstClass.java

  29. The Life Cycle of an Object Creating Objects Using Objects Cleaning Up Unused Objects

  30. Creating Objects • Rectangle r = new Rectangle(5,5,100,200); • Declaration: Rectangle r (Type name) • Instantiation: new • Allocate memory for the object • Initialize instance variables • Call a constructor • Initialization by Calling a Constructor • Rectangle(5,5,100,200)

  31. Using Objects • Manipulate or inspect its variables • objectReference.variable • r.x = 50 • r.y = 80 • Call its methods • objectReference.methodName(argumentList) • r.move(20,30) • Java provides an access control mechanism whereby classes can restrict or allow access to its variables and methods.

  32. Clean Up • When all references to an object are dropped, the object is no longer required, and become eligible for garbage collection. • Call finalize() to release system resources such as open files or open sockets before the object is collected. • Release references to other objects • protected void finalize() throws Throwable • Rectangle2.java

  33. Controlling Access to Members of a Class

  34. class Alpha { private int iamprivate; private void privateMethod() { System.out.println ("privateMethod"); } } class Beta { void accessMethod() { Alpha a = new Alpha(); a.iamprivate = 10; a.privateMethod(); } } Private How About one Alpha object access the private member of another Alpha object?

  35. package Greek; class Alpha { protected int iamprotected; protected void protectedMethod() { System.out.println ("protectedMethod"); } } package Greek; class Gamma { void accessMethod() { Alpha a = new Alpha(); a.iamprotected = 10; a.protectedMethod(); } } Protected

  36. Protected (II) import Greek.*; package Latin; class Delta extends Alpha { void accessMethod(Alpha a, Delta d) { a.iamprotected = 10; d.iamprotected = 10; a.protectedMethod(); d.protectedMethod(); } }

  37. package Greek; public class Alpha { public int iampublic; public void publicMethod() { System.out.println("publicMethod"); } } import Greek.*; package Roman; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iampublic = 10; a.publicMethod(); } } Public

  38. package Greek; class Alpha { int iampackage; void packageMethod() { System.out.println("packageMethod"); } } package Greek; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iampackage = 10; a.packageMethod(); } } Package

  39. Inhereitance in Java A mechanism you can use to create a new class by extending the definition of another class Increase the reusability of codes Single Inheritance

  40. Simple Class Hierarchy Object Vehicle Building Car Bike House Office Van Truck Superclass and Subclass

  41. More About Inheritance • Use the extends keyword to create a subclass. • Method Override • Use method overriding when you need a subclass to replace a method of its superclass. • Define a new method that replaces the superclass method that has the same signature. • Calling Superclass Methods • super.<method>(parameters) • super(parameters) (Calling superclass constructors) • Example: GraphicsProgram.java

More Related