Inheritance Flashcards
Describe inheritance
Inheritance is the concept of one class using the variables and methods of another class as if they were their own. Different classes can extend the same object behaving in a different manner or reusing code.
What are the two most common reasons to use inheritance
(1) Code reuse (2) polymorphism
Describe polymorphism
Polymorphism literally means “many forms” and is the ability of a class at runtime to behave differently depending on which class is instantiated.
Describe IS-A relationships and what support Java provides for them.
An object IS-A another object if it does 1 of 2 things - extends that object or implements that interface type. The support Java provides therefore is through “extends” and “implements” e.g. inheritance and interfaces.
Describe HAS-A relationships and what support does Java provide for them.
HAS-A relationships occur between objects when one object has a reference to another object. IS-A and HAS-A relationships are highlighted with protected classes. A sub-class of a protected class has a IS-A relationship with that super class, However, if it is not in the same package it cannot have a HAS-A relationship with that class.
HAS-A relationships promote delegation of functionality into specialized classes.
HAS-A relationships encourage programmers to specialize their classes promotign code reuse and good OO principles.
What is the definition of a polymorphic class?
Any class that has more than one IS-A class.
When are methods bound to its object?
Methods are bound at runtime. Static methods and variables are bound at compile time.
What is the difference between the type of the reference and the type of the object it points to?
A a = new B();
a is the reference and its type is A, but the object it refers to is of type B. One is bound at compile time and the other is bound at runtime. The compiler simply never looks at the actual instance type of the object - only its reference type.
When overriding a method, which can have more or less restrictive access? The method being overriden, or the method in the subclass, and why?
The method being overriden cannot have more restrictive access than the method in the subclass. e.g. if the superclass method is public, then the subclass method cannot be private, protected or default. This is because the program will crash at runtime. The subclass method, however, can have a more visible access modifier than its super class.
What are the 10 rules for overriding a method?
(1) The parameter list must match the overriden method
(2) The return type must either be the same or a subtype of the overriden method.
(3) The access level of the subclass method cannot be less visible than the super class method; it can be more visible.
(4) The super class method must be visible to the child class - final, and private do not make it visible!
(5) The subclass method can throw any unchecked exceptions regardless of whether the super class method throws them or not.
(6) The subclass method can only throw checked exceptions that are the same or a subtype of the super class thrown checked exceptions
(7) The overriding method does not have to throw checked exceptions if the superclass method does.
(8) You cannot override a method marked final - you can see it, but not override it.
(9) You cannot override a static method (you can shadow it however)
(10) You cannot override a method you can’t inherit.
When do you want to call the super class method?
In the scenario in which you do not want to replace the superclass method, but you want to add to it. The super class method call must be the first line in the method.
Why do you need to be careful when not declarign the checked exceptions of a superclass in a overriding method?
When overriding a method that throws checked exceptions you have to be careful if you choose not to throw those checked exceptions in your subclass. A a = new B() and then calling the overriden method on B will fail to compile if the method in B does not declare the checked exceptions. This is because the reference and object type are different and the compiler is expecting the exceptions to be thrown.
What are the rules for creating overloaded methods?
(1) Overloaded methods MUST change the parameter list
(2) They can change the return type
(3) Can change teh access modifier
(4) can declare new or broader checked exceptions
(5) A method can be declared in the same class or a subclass
What are the rules to determine whether something is bound at runtime or compile time with respect to methods?
private, static and final methods are always compile-time bound because they cannot be overriden. Overriden methods are bound dynamically and overloaded methods are bound statically.
Describe the rules for overloading
(1) Name is kept the same
(2) Argument list must change
(3) The return type, exceptions and access can change
(4) Reference type determines which overloaded method to use.