inheritence Flashcards

1
Q

what is inheritance

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

why is inheritance great

A

allows code to be reused without having to copy it into the definitions of derived classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

format of class drawings for inheritance

A

top class - superclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to create derived classes

A
extends baseclass
public class HourlyEmployeeextends Employee{
    /*new instance variables*/
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what does the derived class automatically inherit from the parent class

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what does it mean to override a method definition

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

can you change the return type of an overriden method?

A

when overriding a method the returned type may be changed to any descendent of the returned type - known as a covariant return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

can you change the access permission of an overridden method

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

overriding vs overloading

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does the final modifier do?

A
final public void somemethod() --> this method cannot be redefined in any derived class
final class --> no subclasses of this class can be made...ever
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the super constructor?A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what happens if the base class doesn’t use super in the constructor definition?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to use an original method in a derived class

A

super.someOrigMethod() - the grandchild class goes to the parent and the parent will have a reference to all the methods in the grandparent class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what does instanceof do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does getClass do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what happens when you leave out the modifier?

A

you get package access

17
Q

what is package access?

A

Instance variables or methods having package access can be accessed by name inside the definition of any class in the same package–However, neither can be accessed outside the package

18
Q

what is protected access?

A

can be accessed by name: inside own class def, inside any derived class, in the definition of any class in the same package