Object-Oriented-Programming.txt
Document Details
Uploaded by Deleted User
Full Transcript
Object – Oriented Programming Four Concepts of OOP What is Object - Oriented Programming? Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure...
Object – Oriented Programming Four Concepts of OOP What is Object - Oriented Programming? Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages, including JavaScript, C++, Java, and Python. What is Object - Oriented Programming? OOP languages are not necessarily restricted to the object-oriented programming paradigm. Some languages, such as JavaScript, Python, and PHP, all allow for both procedural and object-oriented programming styles. A class is an abstract blueprint that creates more specific, concrete objects. What is Object - Oriented Programming? Classes often represent broad categories, like Car or Dog that share attributes. These classes define what attributes an instance of this type will have, like color, but not the value of those attributes for a specific object. Classes can also contain functions called methods that are available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific object type. What is Object - Oriented Programming? For example, our Car class may have a repaint method that changes the color attribute of our car. This function is only helpful to objects of type Car, so we declare it within the Car class, thus making it a method. What is Object - Oriented Programming? Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like myCar or goldenRetriever. Each object can have unique values to the properties defined in the class. What is Object - Oriented Programming? For example, say we created a class, Car, to contain all the properties a car must have, color, brand, and model. We then create an instance of a Car type object, myCar to represent my specific car. We could then set the value of the properties defined in the class to describe my car without affecting other objects or the class template. We can then reuse this class to represent any number of cars. What is Object - Oriented Programming? Structures of Object - Oriented Programming Object-oriented programming contains various structures, known as the building blocks of OOP. These structures include: Class: A class is a data type that provides a framework for creating objects. You can define a class to create multiple objects without writing additional code. Object: In OOP, an object represents an instance, or creation, of a class. Objects define specific data, such as properties and behaviors, to implement code. Method: A method is a function that performs a task or action. For example, a method may return information about an object's data. Attribute: This structure stores information about an object and defines its state. You can define an attribute as part of the class. Benefits of Object - Oriented Programming OOP models complex things as reproducible, simple structures Reusable, OOP objects can be used across programs Polymorphism allows for class-specific behavior Easier to debug, classes often contain all applicable information to them Securely protects sensitive information through encapsulation 4 Concepts of Object - Oriented Programming Object-oriented programming has four basic concepts: encapsulation, abstraction, inheritance and polymorphism. Knowing how they work together can help you understand the basic functionality of an OOP computer program. Review the explanation for each concept and explore examples to help you understand them: Encapsulation Encapsulation means to enclose data by containing it within an object. In OOP, encapsulation forms a barrier around data to protect it from the rest of the code. You can perform encapsulation by binding the data and its functions into a class. This action conceals the private details of a class and only exposes the functionality essential for interfacing with it. When a class doesn't allow direct access to its private data, it's well-encapsulated. Encapsulation Example When creating a class to represent a person, you may define private data, such as the person's Social Security Number. You can encapsulate this data as a private variable in the class, which means outside code can't access it. If you write a method in the person class to perform a bank transaction, the function could access the data variable as necessary. In this example, the person's private data is well-encapsulated within the class Encapsulation Encapsulation Abstraction Abstraction refers to using simplified classes, rather than complex implementation code, to access objects. Often, it's easier to design a program when you can separate the interface of a class from its implementation. In OOP, you can abstract the implementation details of a class and present a clean, easy-to-use interface through the class member functions. Abstraction helps isolate the impact of changes made to the code so if an error occurs, the change only affects the implementation details of a class and not the outside code. Abstraction Example 1 Consider a stereo system as an object with a complex logic board on the inside. The stereo system has buttons on the outside to allow for interaction with the object. When you press a button, the logic board completes a function to turn on the system, even though you can't see what happens inside the board. This example represents the concept of abstraction, which you can apply widely in object-oriented programming. Abstraction Example 2 In OOP, you might define a class to represent the human body. You can define some functions as part of its public interface, such as walking or eating food. Using abstraction, it's not necessary to write code to explain the bodily functions allowing a person to walk or eat. Instead, you can create simple functions abstracted from the end user to represent this information. Abstraction Abstraction Abstraction Abstract Class Interface Class ABSTRACTION Inheritance Most object-oriented languages support inheritance, which means a new class automatically inhabits the same properties and functionalities as its parent class. Inheritance allows you to organize classes into hierarchies, where a class might have one or more parent or child classes. If a class has a parent class, it means the class has inherited the properties of the parent. The child class can also modify or extend the behavior of its parent class. Inheritance allows you to reuse code without redefining the functions of a child class. Inheritance Inheritance Inheritance Example For instance, in the animal world, an insect may belong to an insect class. In this class, all insects share similar properties, such as having six legs and an exoskeleton. You can create subclasses for different varieties of insects, such as grasshoppers and ants. These child classes inherit the properties of the insect class, meaning they share those same features. Single Inheritance Multilevel Inheritance Hierarchical Inheritance Multiple Inheritance Inheritance Polymorphism Polymorphism refers to creating objects with shared behaviors. In OOP, polymorphism allows for the uniform treatment of classes in a hierarchy. When you write code for objects at the root of the hierarchy, any objects created by a child class within the hierarchy have the same functions. Depending on the type of object, it may execute different behaviors. Polymorphism Example If you have a class called animal with two child classes, cat and dog, you can create a class function to make a noise. Using polymorphism, you can override this function inherited by the cat and dog child classes. Instead, you can make this function "meow" and "bark" for cat and dog, respectively. Depending on the type of animal object that passes through the interface, it either makes a "meow" or a "bark" noise. Polymorphism Polymorphism Polymorphism Method Overloading Method overloading in java is a feature that allows a class to have more than one method with the same name, but with different parameters. Java supports method overloading through two mechanisms: By changing the number of parameters. Method Overloading Method Overloading Method Overriding Method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes. Method Overriding Method Overriding Method Overriding VS. Method Overloading Method Overriding VS. Method Overloading