inheritence Flashcards
what is inheritance
first a very general class is defined then more specialized versions of the class are defined - you can then add new instance vars and methods - this class can inherit the methods and instance vars of the general class
Inheritance is the process by which a new class is created from another class –The new class is called a derived(child) class, subclass, specialized class –The original class is called the base(parent) class, super class, generalized class - superscript used for super class
why is inheritance great
allows code to be reused without having to copy it into the definitions of derived classes
format of class drawings for inheritance
top class - superclass
how to create derived classes
extends baseclass public class HourlyEmployeeextends Employee{ /*new instance variables*/ }
what does the derived class automatically inherit from the parent class
instance vats
all static vars
all public methods
*private instance variables cannot be changes without using the set and get methods that should be found in the parent class
what does it mean to override a method definition
you can change or override an inherited method if needed –In order to override a method definition, a new definition of the method is simply placed in the class definition, just like any other method that is added to the derived class
can you change the return type of an overriden method?
when overriding a method the returned type may be changed to any descendent of the returned type - known as a covariant return type
can you change the access permission of an overridden method
valid:
base class: private void doSomething()
derived class: public void doSomething()
invalid:
base class: public void doSomething()
derived class: private void doSomething()
A subclass can make something more visible, but not less visible
overriding vs overloading
When a method is overridden, the new method definition given in the derived class has the exact same number and types of parameters as in the base class •When a method in a derived class has a different signature from the method in the base class, that is overloading
what does the final modifier do?
final public void somemethod() --> this method cannot be redefined in any derived class final class --> no subclasses of this class can be made...ever
what is the super constructor?A
a derived class uses a constructor from the base class to initialize all the data inherited from the base class
public derivedClass(int p1, int p2, double p3) { super(p1, p2); instanceVariable = p3;}
–In the above example, super(p1, p2); is a call to the base class constructor - calling super must be the first step in the constructor definition
what happens if the base class doesn’t use super in the constructor definition?
then the no-argument constructor of the base class will automatically be invoked–This can result in an error if the base class has not defined a no-argument constructor - you cannot use multiple supers i.e. super.super.tosString()
how to use an original method in a derived class
super.someOrigMethod() - the grandchild class goes to the parent and the parent will have a reference to all the methods in the grandparent class
what does instanceof do?
checks if an object is of the type given as its second argument - will return true if the object is the type of any descendant of classname
what does getClass do?
if two objects were created with the same class?
Checks to see if the classes are exactly the same
E.g. if one is technical and one is engineer, even though engineer is technical it will return false because they arent exactly the same
Returns a number when used in another way