FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Method_(computer_programming)

Method (computer programming)

Contents
    Reabstraction
        Example
    Copy-assignment operators
    Operator methods
See also
Notes

A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.

Data is represented as properties of the object, and behaviors are represented as methods. For example, a Window object could have methods such as open and close, while its state (whether it is open or closed at any given point in time) would be a property.

In class-based programming, methods are defined within a class, and objects are instances of a given class. One of the most important capabilities that a method provides is method overriding - the same name (e.g., area) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. A method in Java programming sets the behavior of a class object. For example, an object can send an area message to another object and the appropriate formula is invoked whether the receiving object is a Rectangle, Circle, Triangle, etc.

Methods also provide the interface that other classes use to access and modify the properties of an object; this is known as encapsulation. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.

publicclass RectangleextendsShape{ @Override intarea(intheight,intwidth){ returnheight*width; } }

Reabstraction

edit

If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. This is called reabstraction.

In practice, this is rarely used.

Example

edit

In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)

// called like: Math.max(a,b); // declared like: publicclass Math{ // ... publicstaticdoublemax(doublea,doubleb){ return(a<b)?b:a; } }

This static method has no owning object and does not run on an instance. It receives all information from its arguments.

Copy-assignment operators

edit

Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type.

Operator methods

edit

Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ example:

base = std::make_unique<Base>();\n unique_ptr<Base> derived = std::make_unique<Derived>();\n\n base->getMessage(); // Calls Base::getMessage().\n derived->getMessage(); // Calls Derived::getMessage().\n}\n"}}'>importstd; usingstd::unique_ptr; classBase{ public: virtual~Base()=default; virtualvoidgetMessage(){ std::println("I'm the super class!"); } }; classDerived:publicBase{ public: voidgetMessage()override{ std::println("I'm the subclass!); } }; intmain(){ unique_ptr<Base>base=std::make_unique<Base>(); unique_ptr<Base>derived=std::make_unique<Derived>(); base->getMessage();// Calls Base::getMessage(). derived->getMessage();// Calls Derived::getMessage(). }

See also

edit

Notes

edit
Meyer, Bertrand (1988). Object-Oriented Software Construction. Cambridge: Prentice Hall International Series in Computer Science. pp. 52–54. ISBN 0-13-629049-3.
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home