Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Class (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Taxonomy== There are many categories of classes, some of which overlap. ===Abstract and concrete=== <span class="anchor" id="Abstract_and_concrete_classes"></span><span class="anchor" id="Abstract_and_Concrete"></span> {{see also|Abstract type}} In a language that supports inheritance, an ''abstract class'', or ''abstract base class'' (''ABC''), is a class that cannot be directly instantiated. By contrast, a ''concrete class'' is a class that {{em|can}} be directly instantiated. Instantiation of an abstract class can occur only indirectly, via a concrete {{em|sub}}class. An abstract class is either labeled as such explicitly or it may simply specify ''[[abstract method]]s'' (or ''[[virtual method]]s''). An abstract class may provide implementations of some methods, and may also specify virtual methods via [[Type signature|signatures]] that are to be implemented by direct or indirect descendants of the abstract class. Before a class derived from an abstract class can be instantiated, all abstract methods of its parent classes must be implemented by some class in the derivation chain.<ref name="cpppoly">{{cite web|url=http://www.cplusplus.com/doc/tutorial/polymorphism/ |title=Polymorphism|work=C++ Language Tutorial|publisher=cplusplus.com|access-date=2012-05-02}}</ref> Most object-oriented programming languages allow the programmer to specify which classes are considered abstract and will not allow these to be instantiated. For example, in [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]] and [[PHP]], the keyword ''abstract'' is used.<ref>{{cite web| url=http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html|title=Abstract Methods and Classes|work=The Java Tutorials|publisher=Oracle|access-date=2012-05-02}}</ref><ref>{{cite web|url=http://php.net/manual/en/language.oop5.abstract.php|title=Class Abstraction|work=PHP Manual|publisher=The PHP Group|access-date=2012-05-02}}</ref> In [[C++]], an abstract class is a class having at least one abstract method given by the appropriate syntax in that language (a pure virtual function in C++ parlance).<ref name=cpppoly/> A class consisting of only pure virtual methods is called a ''pure abstract base class'' (or ''pure ABC'') in C++ and is also known as an ''interface'' by users of the language.<ref name=cppinterface/> Other languages, notably Java and C#, support a variant of abstract classes called an [[Interface (Java)|interface]] via a keyword in the language. In these languages, [[multiple inheritance]] is not allowed, but a class can implement multiple interfaces. Such a class can only contain abstract publicly accessible methods.<ref name=javainterface/><ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/ms173156.aspx |title=Interfaces (C# Programming Guide) |work=C# Programming Guide |publisher=Microsoft |access-date=2013-08-15}}</ref><ref> {{cite web |url=http://msdn.microsoft.com/en-us/library/ms173149.aspx |title=Inheritance (C# Programming Guide) |work=C# Programming Guide |publisher=Microsoft |access-date=2012-05-02}}</ref> <!--Abstract classes defined as interfaces are a much more specific use of the more general meaning of the term ''interface'', even as used in computer science, and the concept of interfaces has seen much use and popularity within the realm of languages that support object-orientation.--> ===Local and inner=== In some languages, classes can be declared in [[Scope (programming)|scopes]] other than the global scope. There are various types of such classes. An ''[[inner class]]'' is a class defined within another class. The relationship between an inner class and its containing class can also be treated as another type of class association. An inner class is typically neither associated with instances of the enclosing class nor instantiated along with its enclosing class. Depending on the language, it may or may not be possible to refer to the class from outside the enclosing class. A related concept is ''inner types'', also known as ''inner data type'' or ''nested type'', which is a generalization of the concept of inner classes. [[C++]] is an example of a language that supports both inner classes and inner types (via ''[[typedef]]'' declarations).<ref>{{cite web |url=http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr061.htm |title=Nested classes (C++ only) |work=XL C/C++ V8.0 for AIX |publisher=IBM |access-date=2012-05-07}}</ref><ref> {{cite web |url=http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr063.htm |title=Local type names (C++ only) |work=XL C/C++ V8.0 for AIX |publisher=IBM |access-date=2012-05-07}}</ref> A ''local class'' is a class defined within a procedure or function. Such structure limits references to the class name to within the scope where the class is declared. Depending on the semantic rules of the language, there may be additional restrictions on local classes compared to non-local ones. One common restriction is to disallow local class methods to access local variables of the enclosing function. For example, in C++, a local class may refer to [[static variable]]s declared within its enclosing function, but may not access the function's [[automatic variable]]s.<ref>{{cite web |url=http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr062.htm |title=Local classes (C++ only) |work=XL C/C++ V8.0 for AIX |publisher=IBM |access-date=2012-05-07}}</ref> ===Metaclass=== {{Main|Metaclass}} A metaclass is a class where instances are classes.{{sfn|Booch|1994|p=133-134}} A metaclass describes a common structure of a collection of classes and can implement a [[design pattern (computer science)|design pattern]] or describe particular kinds of classes. Metaclasses are often used to describe [[software framework|framework]]s.<ref>{{Cite web|url=http://pharo.gforge.inria.fr/PBE1/PBE1ch14.html|title=13 Classes and metaclasses|website=pharo.gforge.inria.fr|access-date=2016-10-31|archive-date=2021-02-24|archive-url=https://web.archive.org/web/20210224193450/http://pharo.gforge.inria.fr/PBE1/PBE1ch14.html|url-status=dead}}</ref> In some languages, such as [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]] or [[Smalltalk]], a class is also an object; thus each class is an instance of a unique metaclass that is built into the language.<ref name="pythondata model"/><ref>{{cite web |url=https://docs.ruby-lang.org/en/master/Class.html |title=class Class}}</ref> {{sfn|Booch|1994|p=134}} The [[Common Lisp Object System]] (CLOS) provides [[Metaobject|metaobject protocols]] (MOPs) to implement those classes and metaclasses.<ref>{{cite web |url=http://www.alu.org/mop/concepts.html#introduction |title=MOP: Concepts |work=The Common Lisp Object System MetaObject Protocol |publisher=Association of Lisp Users |access-date=2012-05-08 |archive-url=https://web.archive.org/web/20101115095930/http://www.alu.org/mop/concepts.html#introduction |archive-date=2010-11-15 |url-status=dead}}</ref> ===Sealed=== <span class="anchor" id="Non-subclassable"></span><span class="anchor" id="Sealed"></span> A sealed class cannot be subclassed. It is basically the opposite of an ''abstract'' class, which must be derived to be used. A sealed class is implicitly ''concrete''. A class is declared as sealed via the keyword {{code|sealed|lang=csharp}} in C# or {{code|final|lang=java}} in Java or PHP.<ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/ms173149.aspx |title=sealed (C# Reference) |work=C# Reference |publisher=Microsoft |access-date=2012-05-08}}</ref><ref> {{cite web |url=http://docs.oracle.com/javase/tutorial/java/IandI/final.html |title=Writing Final Classes and Methods |work=The Java Tutorials |publisher=Oracle |access-date=2012-05-08}}</ref><ref> {{cite web |url=http://php.net/manual/en/language.oop5.final.php |title=PHP: Final Keyword |work=PHP Manual |publisher=The PHP Group |access-date=2014-08-21}}</ref> For example, Java's {{Java|String}} class is marked as ''final''.<ref>{{cite web |url=http://docs.oracle.com/javase/7/docs/api/java/lang/String.html |title=String (Java Platform SE 7) |work=Java Platform, Standard Edition 7: API Specification |publisher=Oracle |access-date=2012-05-08}}</ref> Sealed classes may allow a compiler to perform optimizations that are not available for classes that can be subclassed.<ref>{{cite web |last1=Brand |first1=Sy |title=The Performance Benefits of Final Classes |url=https://devblogs.microsoft.com/cppblog/the-performance-benefits-of-final-classes/ |website=Microsoft C++ team blog |date=2 March 2020 |publisher=Microsoft |access-date=4 April 2020}}</ref> <!-- The following goes without saying, i.e., it says nothing but to hint that "abstract" and "concrete" are an "either but not both" concept. 'abstract' and 'concrete' are defined already above.--> <!-- While it is impossible in any object-oriented language to have a class that is both abstract and concrete, it may be possible to have an abstract partial class --> <!-- The following seemingly belongs in the 'method' article --> <!-- It is also possible not to declare the whole class as such, but only the [[Override (object-oriented programming)|override]] as sealed. This classes are used because of efficiency concerns (can be called like static classes) and security (avoids inadvertent modification of the class semantics).<ref>{{cite web |access-date=2011-08-03 |date=2002-03-25 |first=Hanspeter |last=Mössenböck |page=17 |publisher=Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik |title=Advanced C#: Overriding of Methods |url=http://ssw.jku.at/Teaching/Lectures/CSharp/Tutorial/Part2.pdf}} </ref> --> ===Open<span class="anchor" id="Open class"></span>=== An open class can be changed. Typically, an [[executable program]] cannot be changed by customers. Developers can often change some classes, but typically cannot change standard or built-in ones. In [[Ruby (programming language)#Open classes|Ruby]], all classes are open. In [[Python (programming language)|Python]], classes can be created at runtime, and all can be modified afterward.<ref>{{cite web|title=9. Classes|url=https://docs.python.org/3.3/tutorial/classes.html|website=The Python Tutorial|publisher=Python.org|access-date=3 March 2018|quote=As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.}}</ref> [[Objective-C#Categories|Objective-C categories]] permit the programmer to add methods to an existing class without the need to recompile that class or even have access to its source code. ===Mixin=== Some languages have special support for [[mixin]]s, though, in any language with multiple inheritance, a mixin is simply a class that does not represent an is-a-type-of relationship. Mixins are typically used to add the same methods to multiple classes; for example, a class {{Mono|UnicodeConversionMixin}} might provide a method called {{Mono|unicode_to_ascii}} when included in classes {{Mono|FileReader}} and {{Mono|WebPageScraper}} that do not share a common parent. ===Partial=== In languages supporting the feature, a ''partial class'' is a class whose definition may be split into multiple pieces, within a single [[source code|source-code]] file or across multiple files.<ref name="mspartial">{{Citation|url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods|title=Partial Classes and Methods|work=C# Programming Guide|date=2015-09-19|author1=mairaw|author2=BillWagner|author3=tompratt-AQ|publisher=Microsoft|access-date=2018-08-08}}</ref> The pieces are merged at compile time, making compiler output the same as for a non-partial class. The primary motivation for the introduction of partial classes is to facilitate the implementation of [[Automatic programming|code generators]], such as [[visual designer]]s.<ref name="mspartial"/> It is otherwise a challenge or compromise to develop code generators that can manage the generated code when it is interleaved within developer-written code. Using partial classes, a code generator can process a separate file or coarse-grained partial class within a file, and is thus alleviated from intricately interjecting generated code via extensive parsing, increasing compiler efficiency and eliminating the potential risk of corrupting developer code. In a simple implementation of partial classes, the compiler can perform a phase of [[precompilation]] where it "unifies" all the parts of a partial class. Then, compilation can proceed as usual. <ref>{{cite web | url=https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods | title=Partial Classes and Members - C# }}</ref> Other benefits and effects of the partial class feature include: * Enables separation of a class's interface and implementation code in a unique way. * Eases navigation through large classes within an [[source code editor|editor]]. * Enables [[separation of concerns]], in a way similar to [[aspect-oriented programming]] but without using any extra tools. * Enables multiple developers to work on a single class concurrently without the need to merge individual code into one file at a later time.<!-- (This enabling may be considered by some or most to be a detriment rather than a benefit for SoC can apply to programmers also).--> Partial classes have existed in [[Smalltalk]] under the name of ''Class Extensions'' for considerable time. With the arrival of the [[.NET Framework|.NET framework 2]], [[Microsoft]] introduced partial classes, supported in both [[C Sharp (programming language)|C#]] 2.0 and [[Visual Basic .NET|Visual Basic 2005]]. [[WinRT]] also supports partial classes. <ref>{{Cite web |last=BillWagner |date=2024-11-14 |title=Partial Classes and Methods - C# |url=https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods |access-date=2025-02-06 |website=learn.microsoft.com |language=en-us}}</ref> ===Uninstantiable=== ''Uninstantiable classes'' allow programmers to group together per-class fields and methods that are accessible at runtime without an instance of the class. Indeed, instantiation is prohibited for this kind of class. For example, in C#, a class marked "static" can not be instantiated, can only have static members (fields, methods, other), may not have ''instance constructors'', and is ''sealed''. <ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.100).aspx |title=Static Classes and Static Class Members (C# Programming Guide) |work=C# Programming Guide |publisher=Microsoft |access-date=2012-05-08}} </ref> ===Unnamed=== An ''unnamed class'' or ''anonymous class'' is not bound to a name or identifier upon definition.<ref>{{Cite web|title=Anonymous Classes (The Java Tutorials > Learning the Java Language > Classes and Objects)|url=https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html|access-date=2021-05-13|website=docs.oracle.com}}</ref><ref>{{Cite web|title=PHP: Anonymous classes - Manual|url=https://www.php.net/manual/en/language.oop5.anonymous.php|access-date=2021-08-11|website=www.php.net}}</ref> This is analogous to named versus [[anonymous function|unnamed functions]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)