Changes

Jump to navigation Jump to search
Added discussion of methods
Line 7: Line 7:  
Inheritance is the ability for one class to use the data and methods of an ancestor class (or multiple ancestor classes if multiple inheritance is allowed), rather than having to define those again within a new class.  If class B inherits from class A, then class A is called an ancestor class of B, and class B is called a descendant class of A.
 
Inheritance is the ability for one class to use the data and methods of an ancestor class (or multiple ancestor classes if multiple inheritance is allowed), rather than having to define those again within a new class.  If class B inherits from class A, then class A is called an ancestor class of B, and class B is called a descendant class of A.
   −
=== Polymorphism ===
+
===Polymorphism===
 
Polymorphism allows an object to be used in place of an ancestor object without the code that is using it having to know about the descendant. This means that a complex object can appear to be a simple one. The example below shows that you can have a specific model of car, such as the [[Model T]], which looks like and acts like a normal car.  Polymorphism can be implemented in different ways.  [[Smalltalk]] implemented it via messages.  Most compilers that support OOP implement a virtual method table, which is an array of method pointers as per the CORBA standard.  A descendant class instance can be used in place of any ancestor class instance, but an ancestor class instance cannot be used in place of a descendant class instance.
 
Polymorphism allows an object to be used in place of an ancestor object without the code that is using it having to know about the descendant. This means that a complex object can appear to be a simple one. The example below shows that you can have a specific model of car, such as the [[Model T]], which looks like and acts like a normal car.  Polymorphism can be implemented in different ways.  [[Smalltalk]] implemented it via messages.  Most compilers that support OOP implement a virtual method table, which is an array of method pointers as per the CORBA standard.  A descendant class instance can be used in place of any ancestor class instance, but an ancestor class instance cannot be used in place of a descendant class instance.
    
===Encapsulation===
 
===Encapsulation===
−
Encapsulation means that the data used by a class is defined within the class itself, rather than having to resort to global data.
+
Encapsulation means that the data used by a class is defined within the class itself, rather than the code having to resort to global data.  Further, functions specific to the class, called "methods" are also defined within the class.
    
== Classes and Instances ==
 
== Classes and Instances ==
 
A class is a definition.  In most languages, a class is a data type.  An instance is an instantiation of a class.  It can be considered in terms of a real-world analog: the designs for a pickup truck are akin to a class.  An actual pickup truck build according to those designs is an instance of that class.  The term "object" is often used to refer to either the class, an instance, or the general concept.  The data within an instance is referred to as "instance data".
 
A class is a definition.  In most languages, a class is a data type.  An instance is an instantiation of a class.  It can be considered in terms of a real-world analog: the designs for a pickup truck are akin to a class.  An actual pickup truck build according to those designs is an instance of that class.  The term "object" is often used to refer to either the class, an instance, or the general concept.  The data within an instance is referred to as "instance data".
 +
 +
==Methods==
 +
A method is a function that is encapsulated within a class.  Methods have some level of scope and some mechanism for inheritance.
 +
 +
Different languages support different types of scoping.  Typically, these levels are:
 +
<ul>
 +
<li><b>Private</b>: The method is only visible to methods within the class.</li>
 +
<li><b>Protected</b>: The method is only visible to methods within the class or methods in descendant classes.</li>
 +
<li><b>Public</b>: The method is visible to methods within the class or its descendants, and to any code referencing an instance of the class or one of its descendants.</li>
 +
</ul>
 +
 +
Inheritance mechanisms define how a method interacts with inheritance.
 +
<ul>
 +
<li><b>Non-virtual</b>: The method is not affected by inheritance.  It is simply a method in the class.</li>
 +
<li><b>Virtual</b>: The method can be overridden in a descendant and may be overriding the same method in an ancestor class.</li>
 +
<li><b>Pure virtual</b>: There is no implementation of the method in this class - there is only the method definition.  The purpose is for different descendants to override and implement the method appropriately for those classes.  Compilers typical generate warnings or errors to code that instantiates a class which has pure virtual methods since calling a pure virtual method will result in an error.</li>
 +
</ul>
    
==Constructors and Destructors==
 
==Constructors and Destructors==
SkipCaptcha
292

edits

Navigation menu