1 / 54

Classes and Objects: A Deeper Loop

Delve into classes, objects, encapsulation, inheritance, and polymorphism in OOP. Explore encapsulating attributes and behaviors, controlling access to members, and creating constructors. Learn about composition and static variables. Make the most of overloaded constructors and read-only properties.

thatfield
Download Presentation

Classes and Objects: A Deeper Loop

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. Classes and Objects: A Deeper Loop Ch 10

  2. Object-Oriented Programming • Object • Abstraction of a read-world item (e.g., car) • Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) • Class • Blueprint for instantiating (creating) objects • Many objects may belong to the same class • User-defined, non-built-in type

  3. The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism

  4. Encapsulation • Attributes & behaviors encapsulated (wrapped) into objects • Information hiding • Implementation details hidden within objects • You can drive a car without knowing details of how engine, transmission really work • Modularization!

  5. Method • Implements a behavior of object • E.g., car accelerates, brakes, turns • Describes the mechanisms that actually perform its tasks • Hides from its user the complex tasks that it performs • Method call by its user tells method to perform its task

  6. Instance Variable & Property • Together, represent an attribute of object • E.g., size, shape, color of a car • Instance variable • Actually stores an attribute • May not be directly accessible (information hiding) • Property • Provides indirect, controlled (get and set) access to an attribute

  7. Constructor • Initializes an object of a class • Automatically called when object is instantiated (keyword new) • Has the same name as the class • Has no return type and is not void

  8. Account Class

  9. UML Class Diagram

  10. Test the Account Class

  11. Result

  12. Time Class

  13. Test the Time Class

  14. Controlling Access to Members • A class’s public interface • public methods are services provided to the class’s clients • A class’s implementation details • private variables and methods are not accessible to the class’s clients • Interfaces change less frequently than implementations

  15. The this Reference • Allows an object to access a reference to itself • Non-static methods implicitly use it when referring to instance variables and other methods • Can be explicitly used too • Useful when instance variables are shadowed by local variables or method parameters

  16. Using the this Reference

  17. Overloaded Constructors • Provide multiple constructors with different parameter lists • The this reference can be used to invoke another constructor • No-argument constructor • A constructor invoked without arguments

  18. Default, Parameterless Constructor • Every class must have at least one constructor • If no constructor is declared, the compiler will create a default, paremeterless constructor • If constructors are declared, the compiler will not create a default constructor • Constructor with default parameters may also be parameterless constructor • Can call the constructor without arguments

  19. Class with Overloaded Constructors

  20. Test the Overloaded Constructors

  21. Composition • A class can have references to objects of other classes as members • Sometimes referred to as a has-a relationship

  22. Composition Example

  23. Test Composition

  24. static Variable • Represents class-wide information • Shared by all objects of the class • Accessible even when no object of the class exists • Can be accessed with the class or an object

  25. static Variable Example

  26. Test static Variable

  27. Read-only, Derived Properties • Read-only (write-only) property • Property with only a get (set) method • Derived property • Property that is derived based on the values of other properties

More Related