Oo what is an object




















The attendance attribute is important to keep track of for billing Owners at the end of the month. Methods are how programmers promote reusability, and keep functionality encapsulated inside an object. This reusability is a great benefit when debugging. Learn OOP without scrubbing through videos or documentation. Inheritance allows classes to inherit features of other classes. Put another way, parent classes extend attributes and behaviors to child classes. Inheritance supports reusability.

If basic attributes and behaviors are defined in a parent class, child classes can be created extending the functionality of the parent class, and adding additional attributes and behaviors. For example, herding dogs have the unique ability to herd animals. In other words, all herding dogs are dogs, but not all dogs are herding dogs. We represent this difference by creating a child class HerdingDog from the parent class Dog , and then add the unique herd behavior.

The benefits of inheritance are programs can create a generic parent class, and then create more specific child classes as needed. This simplifies overall programming, because instead of recreating the structure of the Dog class multiple times, child classes automatically gain access to functionalities within their parent class.

In the following code snippet, child class HerdingDog inherits the method bark from the parent class Dog , and the child class adds an additional method, herd. Notice that the HerdingDog class does not have a copy of the bark method, it inherits the bark method defined in the parent Dog class.

When the code calls fluffy. Note: Parent class is also known as super class, or base class. Child class can also be called derived class, or extended class. In JavaScript, inheritance is also known as prototyping. A prototype object acts as a template for another object to inherit properties and behaviors from. There can be multiple prototype object templates, creating a prototype chain. Inheritance is from parent to child.

In our example all three dogs can bark, but only Maisel and Fluffy can herd. The herd method is defined in the child HerdingDog class, so the two objects, Maisel and Fluffy , instantiated from the HerdingDog class have access to the herd method. Rufus is an object instantiated from the parent class Dog , so Rufus only has access to the bark method. Encapsulation means containing all important information inside an object , and only exposing selected information to the outside world.

Attributes and behaviors are defined by code inside the class template. Then, when an object is instantiated from the class, the data and methods are encapsulated in that object. Encapsulation hides the internal software code implementation inside a class, and hides internal data of inside objects. The information the car shares with the outside world, using blinkers to indicate turns, are public interfaces. In contrast, the engine is hidden under the hood. However, exposing internal, private data like the engine temperature, would just confuse other drivers.

Encapsulation adds security. This adds a layer of security, where the developer chooses what data can be seen on an object by exposing that data through public methods in the class definition.

Within classes, most programming languages have public, protected, and private sections. Public is the limited selection of methods available to the outside world, or other classes within the program. Protected is only accessible to child classes. Private code can only be accessed from within that class. Note: JavaScript has private and protected properties and methods. Consider the getAge method in our example code, the calculation details are hidden inside the Dog class.

This allows us to hide important information that should not be changed from both phishing and the more likely scenario of other developers mistakenly changing important data. Encapsulation adds security to code and makes it easier to collaborate with external developers. Instead, developers create public methods that allow other developers to call methods on an object.

Ideally, these public methods come with documentation for the external developers. Abstraction means that the user interacts with only selected attributes and methods of an object. Abstraction uses simplified, high level tools, to access a complex object.

Abstraction is using simple classes to represent complexity. Abstraction is an extension of encapsulation. A driver only uses a small selection of tools: like gas pedal, brake, steering wheel, blinker. The engineering is hidden from the driver. To make a car work, a lot of pieces have to work under the hood, but exposing that information to the driver would be a dangerous distraction.

Abstraction also serves an important security role. By only displaying selected pieces of data, and only allowing data to be accessed through classes and modified through methods , we protect the data from exposure. Polymorphism means designing objects to share behaviors. Using inheritance, objects can override shared parent behaviors, with specific child behaviors.

Polymorphism allows the same method to execute different behaviors in two ways: method overriding and method overloading. So maintaining a large codebase like this for years — with changes along the way — is difficult.

Applying abstraction means that each object should only expose a high-level mechanism for using it. This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects. Think — a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button. Preferably, this mechanism should be easy to use and should rarely change over time. You interact with your phone by using only a few buttons.

You only need to know a short set of actions. Objects are often very similar. They share common logic. So how do we reuse the common logic and extract the unique logic into a separate class? One way to achieve this is inheritance.

It means that you create a child class by deriving from another parent class. This way, we form a hierarchy. The child class reuses all fields and methods of the parent class common part and can implement its own unique part. If our program needs to manage public and private teachers, but also other types of people like students, we can implement this class hierarchy. This way, each class adds only what is necessary for it while reusing common logic with the parent classes.

Say we have a parent class and a few child classes which inherit from it. Sometimes we want to use a collection — for example a list — which contains a mix of all these classes. But each child class keeps its own methods as they are. This typically happens by defining a parent interface to be reused.

It outlines a bunch of common methods. Then, each child class implements its own version of these methods. Any time a collection such as a list or a method expects an instance of the parent where common methods are outlined , the language takes care of evaluating the right implementation of the common method — regardless of which child is passed. Take a look at a sketch of geometric figures implementation.

They reuse a common interface for calculating surface area and perimeter:. Having these three figures inheriting the parent Figure Interface lets you create a list of mixed triangles , circles , and rectangles.

And treat them like the same type of object. Then, if this list attempts to calculate the surface for an element, the correct method is found and executed. The object source element contains the attributes and methods specific to all instance objects.

Figure shows a class and instances being created from it. Methods are the pieces of code that implement the behavior of an object. An object method can access its own data, the instance data and the factory data declared in the factory object source element. A factory method can access its own data and the factory data. Methods can be incomplete; these are known as method prototypes. A method prototype does not contain any code, just a heading and an end-marker.

A method prototype is always fully implemented elsewhere in the application; for an explanation of the benefits of using method prototypes see the section Interfaces. Interfaces are collections of method prototypes. The set of method prototypes defines a common behavior that a variety of objects might share. For example, you might have an interface Rentable, that defines methods appropriate for objects that people can rent, such as cars and video tapes.

The interface methods might include pickUp and dropOff. These are defined as prototypes in the interface. Each class that implements the interface must provide full method definitions for the method prototypes in the interface. Interfaces give you additional flexibility in designing your OO applications; they are one of the elements in an OO language that provide polymorphism see the section Polymorphism. A message is the way you request an object to perform a service.

A message always consists of the following:. Messages may also optionally contain input and output parameters. Where there is an output parameter, the sender will be expecting a reply to its message. The object reference enables the run-time system to find the object for which the message is intended. The target of a message is known as the receiver. This means that the receiver of a message is determined at run time rather than at compile time.

The method selector is the text of the message; it tells the receiver which method it should invoke. Figure A Message. Encapsulation in OO langauges is the inclusion within an object of everything it needs to function: its data and the implementation of its methods.

Other objects can use it as long as they adhere to the object's interface; they do not need to know anything about the way the object holds and manipulates its data. Encapsulation helps to preserve the integrity of data. Inheritance enables OO languages to mirror the real world more closely by establishing hierarchies of classes. Most objects in the real world belong to specific categories; for example, cars and motor bikes are vehicles, managers and workers are employees.

Using inheritance in OO applications reduces coding effort, because you only need to include methods in a subclass if the required behavior is different from that provided in the superclass. When you create a method in a subclass of the same name as a method in the class it inherits from, the method in the subclass overrides the method in the class; the method in the subclass is known as a reimplementation of the method in the class.

For example, our banking application might handle the following types of account: checking accounts, credit card accounts and savings accounts.



0コメント

  • 1000 / 1000