540 likes | 559 Views
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.
E N D
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
The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism
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!
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
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
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
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
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
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
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
Composition • A class can have references to objects of other classes as members • Sometimes referred to as a has-a relationship
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
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