is_a versus has_a - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

is_a versus has_a

Description:

Shirt is a particular kind of clothing. Bald eagle is a species of bird. ... However, the designers of the Java API decided to create a Stack class by making ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 18
Provided by: IBMU362
Learn more at: http://www.cs.ucf.edu
Category:
Tags: hasa | isa | versus

less

Transcript and Presenter's Notes

Title: is_a versus has_a


1
is_a versus has_a
  • Inheritance allows us to specialize a particular
    class. Assume we have a class called Fruit, then
    we could subclass Fruit and create an Apple
    class. This new subclass Apple is a special type
    of Fruit.
  • We say Apple is_a Fruit
  • This type of relationship is called an is_a
    relationship.
  • More examples
  • Automobile is a special case of transportation
    vehicle.
  • Shirt is a particular kind of clothing.
  • Bald eagle is a species of bird.
  • Triangle is a type of polygon.

2
is_a versus has_a
  • Not all relationships between classes are is_a
    relationships.
  • Sometimes we have has_a relationships (also
    called contains_a relationships).
  • Weve already talked about has_a relationships!
    We called it composition (also called
    aggregation).
  • Assume we have a Triangle class. Triangles are
    made up of vertices. We compose a Triangle
    class of Vertex (supplier) classes.
  • We say Triangle has_a Vertex
  • More examples
  • An automobile contains a steering wheel.
  • A shirt contains a pocket.
  • A bald eagle has a wing.

3
is_a versus has_a
  • Inheritance (is_a) and composition (has_a) both
    provide ways to reuse software.
  • In inheritance, we can reuse the superclass.
  • In composition, we can reuse the supplier class.
  • Typically, inheritance should be used only when a
    clear is_a relationship exists.

4
Example Vectors and Stacks
  • A Vector in Java is essentially an array that
    grows in size dynamically.
  • A stack is a data structure that allows items to
    be put in/taken out in a certain way. (It is not
    random access like a Vector).
  • However, the designers of the Java API decided to
    create a Stack class by making it a subclass of a
    Vector.
  • This is poor design it allows users of the Stack
    class access to the methods of the Vector class
    (since those methods were inherited).
  • However, the Vector methods allow random access!
    This means users of the Stack class can access
    items in ways that are not consistent with a
    stacks operation.

5
Inheritance and References
  • The data type of a variable can be
  • a primitive data type, or
  • a reference type (which will hold a reference to
    an object).
  • So, a variable of reference type holds a
    reference to an object.
  • class C1 ...
  • // in some other place
  • C1 x
  • What can be the class of the object which is
    referred by the variable x?
  • A variable of reference type can refer to an
    object of its declared class, or to an object of
    any subclass of its declared class.
  • This means that x can store a reference to an
    object of C1 or an object of any subclass of C1
    (its siblings).

6
Inheritance and References (cont.)
  • Another way of saying it A variable declared to
    be of a superclass type can be bound to an object
    of the same superclass type or any object of a
    subclass type.
  • Yet another way An instance of a subclass can
    appear wherever an instance of its superclass is
    expected.

7
Inheritance and References (cont.)
  • class C1 ...
  • class C2 extends C1 ...
  • class C3 ...
  • // in some other place
  • C1 o1 new C1() C2 o2 new C2() C3 o3 new
    C3()
  • o1 o2 Automatically saved (Widening
    Conversion)
  • o2 (C2) o1 We need explicit type casting
    (Narrowing Conversion)
  • if o1 holds C2 object, this is okay
  • But, if o1 holds C1 object ? run-time error
  • o1 o3 ILLEGAL (There is no inheritance
    relation between C1 and C3)
  • o1 (C1) o3 ILLEGAL (There is no inheritance
    relation between C1 and C3)

8
Inheritance and References (cont.)
  • Assigning a descendant reference to an ancestor
    reference is considered to be a widening
    conversion, and it can be performed by simple
    assignment.
  • Assigning an ancestor reference to a descendant
    reference is considered to be a narrowing
    conversion, and it must be done with an explicit
    type cast operation.
  • If that ancestor reference does not point to a
    descendant object ? run-time error
  • Since Object class is the ancestor of all classes
    in Java, we can store any type of reference in an
    Object reference variable.

9
Inheritance and References (cont.)
  • class C1 ...
  • class C2 extends C1 ...
  • // in some other place
  • C1 o1 new C1() C2 o2 new C2()
  • Object o3
  • o1 o2 Automatically saved (Widening
    Conversion)
  • o2 (C2) o1 We need explicit type casting
    (Narrowing Conversion)
  • This is okay, because o1 holds an C2 object.
  • o3 o1 Automatically saved (Widening
    Conversion)
  • o3 o2 Automatically saved (Widening
    Conversion)

10
Inheritance and References (cont.)
  • So, a variable of reference type holds a
    reference to an object.
  • A variable of reference type may hold references
    to objects of different classes.
  • The class of the object referred to by a
    reference variable cannot always be determined
    at compile time.
  • ? it can be determined only at run time.
  • We can say that any class is a subclass (or
    subtype) of an ancestor class. The subtype
    relationship does not have to be direct. Assume
    Fruit extends Object. Apple extends Fruit.
    GrannySmithApple extends Apple.
  • Then we can say that Fruit is a subtype of
    Object, Apple is a subtype of Object,
    GrannySmithApple is a subtype of Object,
    GrannySmithApple is a subtype of Fruit, etc.

11
Conversion of Types
  • There is a difference between conversions of
    primitive types and reference types
  • During the conversion of a primitive type, the
    representation of the value being converted is
    changed.
  • The conversion of an object reference does not
    affect the representation of the object. The
    identity of object and state remain the same.

12
Subtype Polymorphism
  • Subtype polymorphism the ability of a single
    variable to refer to objects of differing type
  • Rule of Assignment
  • The type of the expression on the right-side of
    an assignment must be
  • the same type as or a subtype of the type of the
    variable on the left-hand side of the assignment.
  • class Student ...
  • class Undergraduate extends Student ...
  • class Graduate extends Student ...
  • Student student1, student2
  • student1 new Undergraduate() // polymorphic
    assignment
  • student2 new Graduate() // polymorphic
    assignment

13
Polymorphism (cont.)
  • Graduate student3
  • Although student2 holds a Graduate object, the
    following statement will be illegal.
  • student3 student2 ? compilation error
  • But, the following will be okay
  • student3 (Graduate) student2
  • student3 (Graduate) student1
  • ? runtime error because student1 holds an
    Undergraduate object.

14
Downcasting
  • Casting a variable to a subtype of its declared
    type is called downcasting.
  • For example, explicit type casting of the
    variable student2 whose declared type is Student
    is downcasting.
  • Graduate student3
  • student3 (Graduate) student2
  • Java allows explicit type casting of any
    reference type T1 to any reference type T2 which
    holds a subclass relation with T1 at compile
    time.
  • The validity of an explicit type casting is
    always checked at run time. If the cast is
    invalid, a ClassCastException will be thrown.
  • In our example, a run-time check will be
    performed to determine whether student2 holds a
    reference to an object that is an instance of
    Graduate or its subclasses. If it is not, a
    ClassCastException will be thrown.

15
Proper Ways of Downcasting
  • Use instanceof operator before downcasting
  • expression instanceof Type
  • returns true if expression is an instance of
    Type (or its subtype).
  • if (student1 instanceof Graduate)
  • Graduate gradStudent (Graduate) student1
  • . . .
  • else // student1 is not a graduate student
  • . . .

16
Proper Ways of Downcasting (cont.)
  • Catch ClassCastException exception
  • try
  • Graduate gradStudent (Graduate) student1
  • . . .
  • catch (ClassCastException e)
  • // student1 is not a graduate student
  • . . .

17
Is Downcasting Needed?
  • If Graduate class defines a method
    getResearchTopic() and this method is not defined
    in Student class, the following code will give a
    compilation error (although s1 holds a Graduate
    object).
  • Student s1 new Graduate()
  • s1.getResearchTopic() ? compilation error.
  • So, we have to downcast s1 to Graduate, before we
    invoke the method.
  • Student s1 new Graduate()
  • Graduate gs (Graduate) s1
  • gs.getResearchTopic()
Write a Comment
User Comments (0)
About PowerShow.com