1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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)
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;
}
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
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
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;
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
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…
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