Polymorphism

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/7

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.

8 Terms

1
New cards

the super Keyword

used to call the constructor method of our super class

  • We can use it to call the constructor in our superclass before initialising our own internal attributes… This guarantees super classes are initialised properly

  • If you choose to use it, it must be the first statement in your constructor. Put any necessary parameters to the constructor in the brackets, just as you would using new()…

  • But if you omit it, then the java compiler will put it in anyway

    • It will assume a default constructor (a constructor with no parameters)

2
New cards

The C++ Equivalent

In C++, we use initializer lists to do the same

  • Use the name of the super class, along with its constructor parameters when we implement the constructor:

Lecturer::Lecturer(string name, int age, string subject) : Person(name, age)
{
	specialistSubject = subject;
}

3
New cards

Polymorphism

  • Any subclass must have all the attributes and methods of its superclass, by definition

  • Also, a subclass can only add functionality to a class… never remove it

  • Therefore, all classes must have all the external behaviour of their super class

  • Therefore, any class can be treated as a type of its super class

  • All instances of a subclass can therefore be:

    • passed as a parameter into methods expecting its super class

    • stored in a variable or array that is typed as its superclass

  • This provides full backward compatibility of a subclass with any code written for its superclass

4
New cards

Polymorphism Application

Any object instance of a class that extends another can be treated as an object of that class

  • An object reference of one type can therefore refer to an object of a different type

  • This is safe, as any subclass will have at least the same methods and instance variables

  • Casting can also be used convert between types of object references but only if the underlying object is the same class or a subclass of the type you are casting to

5
New cards

Casting Object References

  • Java will implicitly and transparently cast an object reference as necessary, provided that the transformation goes upwards the inheritance hierarchy...

  • casting down the hierarchy must be done explicitly

  • it is only the object reference which changes type during casting

    • once an object instance is created, it lives and dies with the same class and cannot be changed

    • polymorphism can therefore be viewed as an ‘overlay’ that only looks at part of an object’s implementation

ChessPiece c, c2;
Prawn p, p2;

p = new Prawn();
c = p;
p2 = (Prawn) c;
float f = 42.42;
int i = (int) f;

6
New cards

Method Overriding

  • used to change behaviour by replacing a method in the superclass

  • implementation is simple: define a method in your class with the same signature as one in a superclass

  • the functionality in this method replaces the functionality inherited from the superclass

7
New cards

Polymorphism and Method Overriding

  • the method called will be the most specific (lowest in the inheritance hierarchy) applicable to that object instance

  • exactly the same functionality as if the object reference were the same type as the object instance

  • only access to the methods and instance variables matching the object reference’s position in the inheritance hierarchy will be accessible

  • enables us to create heterogeneous collections of object instances

    • objects in the aren’t necessary all of the same type, but enough functionality to allow them to be interoperable…

8
New cards

Method Overloading

Not to be confused with Method Overriding

defining multiple methods with the same name but different parameter lists

  • often applied to constructors

  • but can be applied to any method