Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

is-a

From Wikipedia, the free encyclopedia
Subsumption relationship between abstractions
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
This articleis written like apersonal reflection, personal essay, or argumentative essay that states a Wikipedia editor's personal feelings or presents an original argument about a topic. Pleasehelp improve it by rewriting it in anencyclopedic style.(August 2018) (Learn how and when to remove this message)
This articlemay be too technical for most readers to understand. Pleasehelp improve it tomake it understandable to non-experts, without removing the technical details.(August 2018) (Learn how and when to remove this message)
(Learn how and when to remove this message)

Inknowledge representation andontology components, including forobject-oriented programming anddesign,is-a (also written asis_a oris a) is asubsumptive[a] relationship betweenabstractions (e.g.,types,classes), wherein oneclassA is asubclass of another classB (and soB is asuperclass ofA).In other words, type A is asubtype of type B when A'sspecification implies B's specification. That is, any object (or class) that satisfies A's specification also satisfies B's specification, because B's specification is weaker.[1]

For example, a cat 'is a' animal, but not vice versa. All cats are animals, but not all animals are cats.Behaviour that is relevant to all animals is defined on an animal class, whereas behaviour that is relevant only for cats is defined in a cat class. By defining the cat class as 'extending' the animal class, all cats 'inherit' the behaviour defined for animals, without the need to explicitly code that behaviour for cats.

Related concepts

[edit]

Theis-a relationship is to be contrasted with thehas-a (has_a orhas a) relationship between types (classes); confusing the relationshas-a andis-a is a common error when designing a model (e.g., acomputer program) of the real-world relationship between an object and its subordinate. Theis-a relationship may also be contrasted with theinstance-of relationship between objects (instances) and types (classes): seeType–token distinction.

To summarize the relations, there are:

  • hyperonymhyponym (supertype/superclass–subtype/subclass) relations between types (classes) defining a taxonomic hierarchy, where
    • for asubsumption relation: a hyponym (subtype, subclass) has atype-of (is-a) relationship with its hyperonym (supertype, superclass);
  • holonymmeronym (whole/entity/container–part/constituent/member) relations between types (classes) defining a possessive hierarchy, where
    • for anaggregation (i.e. without ownership) relation:
      • a holonym (whole) has ahas-a relationship with its meronym (part),
    • for acomposition (i.e. with ownership) relation:
      • a meronym (constituent) has apart-of relationship with its holonym (entity),
    • for acontainment[2] relation:
      • a meronym (member) has amember-of relationship with its holonym (container);
  • concept–object (type–token) relations between types (classes) and objects (instances), where
    • a token (object) has aninstance-of relationship with its type (class).

Examples of subtyping

[edit]

Subtyping enables a given type to be substituted for another type or abstraction. Subtyping is said to establish anis-a relationship between the subtype and some existing abstraction, either implicitly or explicitly, depending on language support. The relationship can be expressed explicitly via inheritance in languages that support inheritance as a subtyping mechanism.

C++

[edit]

The following C++ code establishes an explicit inheritance relationship between classesB andA, whereB is both a subclass and a subtype ofA, and can be used as anA wherever aB is specified (via a reference, a pointer or the object itself).

classA{public:voidDoSomethingALike()const{}};classB:publicA{public:voidDoSomethingBLike()const{}};voidUseAnA(Aconst&some_A){some_A.DoSomethingALike();}voidSomeFunc(){Bb;UseAnA(b);// b can be substituted for an A.}

[3]

Python

[edit]

The following python code establishes an explicit inheritance relationship between classesB andA, whereB is both a subclass and a subtype ofA, and can be used as anA wherever aB is required.

classA:defdo_something_a_like(self):passclassB(A):defdo_something_b_like(self):passdefuse_an_a(some_a):some_a.do_something_a_like()defsome_func():b=B()use_an_a(b)# b can be substituted for an A.

The following example,type(a) is a "regular" type, andtype(type(a)) is a metatype. While as distributed all types have the same metatype (PyType_Type, which is also its own metatype), this is not a requirement. The type of classic classes, known astypes.ClassType, can also be considered a distinct metatype.[4]

>>>a=0>>>type(a)<type 'int'>>>>type(type(a))<type 'type'>>>>type(type(type(a)))<type 'type'>>>>type(type(type(type(a))))<type 'type'>

Java

[edit]

In Java,is-a relation between the type parameters of one class or interface and the type parameters of another are determined by the extends andimplements clauses.

Using theCollections classes,ArrayList<E> implementsList<E>, andList<E> extendsCollection<E>. SoArrayList<String> is a subtype ofList<String>, which is a subtype ofCollection<String>. The subtyping relationship is preserved between the types automatically. When defining an interface,PayloadList, that associates an optional value ofgeneric type P with each element, its declaration might look like:

interfacePayloadList<E,P>extendsList<E>{voidsetPayload(intindex,Pval);...}

The following parameterizations of PayloadList are subtypes ofList<String>:

PayloadList<String,String>PayloadList<String,Integer>PayloadList<String,Exception>

Liskov substitution principle

[edit]
Main article:Liskov substitution principle

Liskov substitution principle explains a property,"If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T,".[5] Following example shows a violation of LSP.

Here is perhaps an example of violation of LSP:

classRectangle{public:voidSetWidth(doublew){itsWidth=w;}voidSetHeight(doubleh){itsHeight=h;}doubleGetHeight()const{returnitsHeight;}doubleGetWidth()const{returnitsWidth;}doubleGetArea()const{returnGetHeight()*GetWidth();}private:doubleitsWidth;doubleitsHeight;};

From a programing point of view, the Square class may be implemented by inheriting from the Rectangle class.

publicclassSquare:Rectangle{public:virtualvoidSetWidth(doublew);virtualvoidSetHeight(doubleh);};voidSquare::SetWidth(doublew){Rectangle::SetWidth(w);Rectangle::SetHeight(w);}voidSquare::SetHeight(doubleh){Rectangle::SetHeight(h);Rectangle::SetWidth(h);}

However, this violates LSP even though theis-a relationship holds between Rectangle and Square

Consider the following example, where function g does not work if a Square is passed in, and so the open-closed principle might be considered to have been violated.

voidg(Rectangle&r){r.SetWidth(5);r.SetHeight(4);assert(r.GetArea())==20);// assertion will fail}

Conversely, if one considers that the type of a shape should only be a constraint on the relationship of its dimensions, then it is the assumption in g() that SetHeight will change height, and area, but not width that is invalid, not just for true squares, but even potentially for other rectangles that might be coded so as to preserve area or aspect ratio when height changes.

[6]

See also

[edit]

Notes

[edit]
  1. ^SeeLiskov substitution principle.

Citations

[edit]
  1. ^"Subtypes and Subclasses"(PDF). MIT OCW. Retrieved2 October 2012.
  2. ^See alsoContainment (computer programming).
  3. ^Mitchell, John (2002). "10 "Concepts in object-oriented languages"".Concepts in programming language. Cambridge, UK: Cambridge University Press. p. 287.ISBN 0-521-78098-5.
  4. ^Guido van Rossum."Subtyping Built-in Types". Retrieved2 October 2012.
  5. ^Liskov, Barbara (May 1988).Data Abstraction and Hierarchy(PDF). SIGPLAN Notices. Archived from the original on Jun 21, 2020.
  6. ^"The Liskov Substitution Principle"(PDF). Robert C. Martin, 1996. Archived fromthe original(PDF) on 5 September 2015. Retrieved2 October 2012.

References

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Is-a&oldid=1265707982"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp