Object lifecycle As an
instance of a class, an object is constructed from a class via
instantiation. Memory is allocated and initialized for the object state and a
reference to the object is provided to consuming code. The object is usable until it is destroyed its state memory is de-allocated. Most languages allow for custom logic at lifecycle events via a
constructor and a
destructor.
Type An object expresses
data type as an interface the type of each member variable and the signature of each
member function (method). A class defines an implementation of an interface, and instantiating the class results in an object that exposes the implementation via the interface. In the terms of type theory, a class is an implementationa
concrete data structure and collection of subroutineswhile a type is an
interface. Different (concrete) classes can produce objects of the same (
abstract) type (depending on type system). For example, the type (interface) might be implemented by that is fast for small stacks but scales poorly and that scales well but has high overhead for small stacks.
Structure notation for classes A class contains
data field syntactically described (or
properties,
fields,
data members, or
attributes). Some programming languages such as Eiffel support specification of
invariants as part of the definition of the class, and enforce them through the type system.
Encapsulation of state is necessary for being able to enforce the invariants of the class.
Behavior The behavior (or action
Interface Every class
implements (or
realizes) an interface by providing
structure and behavior. Structure consists of data and state, and behavior consists of code that specifies how methods are implemented. There is a distinction between the definition of an interface and the implementation of that interface; however, this line is blurred in many programming languages because class declarations both define and implement an interface. Some languages, however, provide features that separate interface and implementation. For example, an
abstract class can define an interface without providing an implementation. Languages that support class inheritance also allow classes to inherit interfaces from the classes that they are derived from. For example, if "class Z" inherits from "class Y" and if "class Y" implements the interface "interface X" then "class Z" also implements the functionality(constants and methods declaration) provided by "interface X". In languages that support
access specifiers, the interface of a class is considered to be the set of public members of the class, including both methods and attributes (via implicit
getter and setter methods); any private members or internal data structures are not intended to be depended on by external code and thus are not part of the interface. Object-oriented programming methodology dictates that the operations of any interface of a class are to be independent of each other. It results in a layered design where clients of an interface use the methods declared in the interface. An interface places no requirements for clients to invoke the operations of one interface in any particular order. This approach has the benefit that client code can assume that the operations of an interface are available for use whenever the client has access to the object. ; Interface example The buttons on the front of your television set are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to toggle the television on and off. In this example, your particular television is the instance, each method is represented by a button, and all the buttons together compose the interface (other television sets that are the same model as yours would have the same interface). In its most common form, an interface is a specification of a group of related methods without any associated implementation of the methods. A television set also has a myriad of
attributes, such as size and whether it supports color, which together comprise its structure. A class represents the full description of a television, including its attributes (structure) and buttons (interface). Getting the total number of televisions manufactured could be a
static method of the television class. This method is associated with the class, yet is outside the domain of each instance of the class. A static method that finds a particular instance out of the set of all television objects is another example.
Member accessibility The following is a common set of
access specifiers: •
Private (or
class-private) restricts access to the class itself. Only methods that are part of the same class can access private members. •
Protected (or
class-protected) allows the class itself and all its subclasses to access the member. •
Public means that any code can access the member by its name. Although many object-oriented languages support the above access specifiers, their semantics may differ. Object-oriented design uses the access specifiers in conjunction with careful design of public method implementations to enforce class invariants—constraints on the state of the objects. A common usage of access specifiers is to separate the internal data of a class from its interface: the internal structure is made private, while public
accessor methods can be used to inspect or alter such private data. Access specifiers do not necessarily control
visibility, in that even private members may be visible to client external code. In some languages, an inaccessible but visible member may be referred to at runtime (for example, by a pointer returned from a member function), but an attempt to use it by referring to the name of the member from the client code will be prevented by the type checker. The various object-oriented programming languages enforce member accessibility and visibility to various degrees, and depending on the language's
type system and compilation policies, enforced at either
compile time or
runtime. For example, the
Java language does not allow client code that accesses the private data of a class to compile. In the
C++ language, private methods are visible, but not accessible in the interface; however, they may be made invisible by explicitly declaring fully abstract classes that represent the interfaces of the class. Some languages feature other accessibility schemes: •
Instance vs. class accessibility:
Ruby supports
instance-private and
instance-protected access specifiers in lieu of class-private and class-protected, respectively. They differ in that they restrict access based on the instance itself, rather than the instance's class. •
Friend: C++ supports a mechanism where a function explicitly declared as a
friend function of the class may access the members designated as private or protected. •
Path-based: Java supports restricting access to a member within a
Java package, which is the logical path of the file. However, it is a common practice when extending a Java framework to implement classes in the same package as a framework class to access protected members. The source file may exist in a completely different location, and may be deployed to a different file, yet still be in the same logical path as far as the JVM is concerned. One important question when modeling and implementing a system of object classes is whether a class can have one or more superclasses. In the real world with actual sets, it would be rare to find sets that did not intersect with more than one other set. However, while some systems such as Flavors and CLOS provide a capability for more than one parent to do so at run time introduces complexity that many in the object-oriented community consider antithetical to the goals of using object classes in the first place. Understanding which class will be responsible for handling a message can get complex when dealing with more than one superclass. If used carelessly this feature can introduce some of the same system complexity and ambiguity classes were designed to avoid. Most modern object-oriented languages such as Smalltalk and Java require single inheritance at run time. For these languages, multiple inheritance may be useful for modeling but not for an implementation. However,
semantic web application objects do have multiple superclasses. The volatility of the Internet requires this level of flexibility and the technology standards such as the
Web Ontology Language (OWL) are designed to support it. A similar issue is whether or not the class hierarchy can be modified at run time. Languages such as Flavors, CLOS, and Smalltalk all support this feature as part of their
meta-object protocols. Since classes are themselves first-class objects, it is possible to have them dynamically alter their structure by sending them the appropriate messages. Other languages that focus more on strong typing such as Java and C++ do not allow the class hierarchy to be modified at run time. Semantic web objects have the capability for run time changes to classes. The rationale is similar to the justification for allowing multiple superclasses, that the Internet is so dynamic and flexible that dynamic changes to the hierarchy are required to manage this volatility. Although many class-based languages support inheritance, inheritance is not an intrinsic aspect of classes. An
object-based language (i.e.
Classic Visual Basic) supports classes yet does not support inheritance. == Inter-class relationships ==