Polymorphism

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

11 Terms

1
New cards

“Object” ref var

Object o;

  • a reference of type Object can be used to store any object

  • can be used to acquire polymorphic behaviours

2
New cards

.equals method in Object class

  • compares the content of two objects of the same class (logical equality)

  • can be manipulated to work for any pairs of any kind of objects

3
New cards

(kind of) general recipe of the .equals method

(this is for the Account class made in lecture)

  1. check if the given obj is ull

  2. check if the given obj and “this obj” is of the same class

  3. ***type cast the obj to be Account (Account other = (Account)o)

  4. for each instance var, ensure that they are equal

    1. for prim types, use ==

    2. for ref types (most notably String) use equals method

    3. check null again, this time for the instance ref vars (like String)

<p>(this is for the Account class made in lecture) </p><ol><li><p>check if the given obj is ull</p></li><li><p>check if the given obj and “this obj” is of the same class</p></li><li><p>***type cast the obj to be Account (Account other = (Account)o)</p></li><li><p>for each instance var, ensure that they are equal</p><ol><li><p>for prim types, use ==</p></li><li><p>for ref types (most notably String) use equals method </p></li><li><p>check null again, this time for the instance ref vars (like String)</p></li></ol></li></ol><p></p>
4
New cards

instanceof

Suppose you have a statement “s instanceofT T”

  • returns false if the val of s == null

  • returns true if the class of obj s is compatible with the type T at runtime, so either:

    • T is a class, and s is an instance of T or an instance of a subclass of T

    • T is an interface, and s is an instance of a class that implements T

5
New cards

before typecasting (vv important for .equals method and other methods that compare objs with each other ), ALWAYS:

  • PRECEDE with the “instanceof” operator

<ul><li><p>PRECEDE with the “instanceof” operator </p></li></ul><p></p>
6
New cards

whenever an element is removed from the “generic” data structure, the reference must be ____

cast to the original type of the object.

7
New cards

advantage and drawback of generic data structures

advantage: a single implementation can be used in a variety of contexts

drawback: the type of the return value of the access methods (getters) is Object, consequently requiring type casting back to its proper specialized type THUS we need Generics

8
New cards

Generics

  • a concept unique to java

  • introduces CLASS parameters (whereas we only had parameters for methods before)

  • see keyword in image → <T> reps the Type of objs to be stored in the class (Pair in this case)

<ul><li><p>a concept unique to java</p></li><li><p>introduces CLASS parameters (whereas we only had parameters for methods before)</p></li><li><p>see keyword in image → &lt;T&gt; reps the Type of objs to be stored in the class (Pair in this case)</p></li></ul><p></p>
9
New cards

the body of a parametized class

knowt flashcard image
10
New cards

type declaration involving a parametized class

now, typecasting would not be necessary for the generic data structure known as Pair, so the type validation step done at compile-tiime would not be sacrificed but it can enjoy the advantage of being a generic data structure

  • a Pair declared with Pair<Integer> can only store Integer objs

  • helps detect errors at compile time

<p>now, typecasting would not be necessary for the generic data structure known as Pair, so the type validation step done at compile-tiime would not be sacrificed but it can enjoy the advantage of being a generic data structure</p><ul><li><p>a Pair declared with Pair&lt;Integer&gt; can only store Integer objs</p></li><li><p>helps detect errors at compile time </p></li></ul><p></p>
11
New cards

defining a generic type cont’d (for Pair class)

now the first item and the second item in the pair do not have to be the same type

<p>now the first item and the second item in the pair do not have to be the same type </p>

Explore top flashcards

CGT 163 Exam 1
Updated 967d ago
flashcards Flashcards (81)
Chapter 1 & 2
Updated 231d ago
flashcards Flashcards (65)
Body Mechanics
Updated 197d ago
flashcards Flashcards (34)
Biology term 1 y9
Updated 883d ago
flashcards Flashcards (49)
CGT 163 Exam 1
Updated 967d ago
flashcards Flashcards (81)
Chapter 1 & 2
Updated 231d ago
flashcards Flashcards (65)
Body Mechanics
Updated 197d ago
flashcards Flashcards (34)
Biology term 1 y9
Updated 883d ago
flashcards Flashcards (49)