Inheritance Flashcards

1
Q

Inheritance

A

Enables a class to take on the properties and methods defined in another class. A subclass will inherit visible properties and methods from the superclass while adding members of its own.

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

Subclass

A

Is the derived class acquiring the properties and behaviors from another class.

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

Superclass

A

Is the base class whose members are being passed down.

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

Inheritance characteristics

A

Except for Object which is the starting ancestor of all classes in Java, ALL classes in Java have ONE and ONLY ONE direct superclass, which is called Single Inheritance. If a class does not have an explicit superclass, then it is implicitly a subclass of Object.

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

Inheritance principles

A
  1. A subclass inherits all visible (non-private) properties and behaviors (methods) from the superclass.
  2. A subclass DOES NOT inherit private properties or methods from the superclass.
  3. Constructors are NOT inherited.
  4. The subclass will then pass these traits through each subsequent generation if it becomes a superclass to its own subclasses.
  5. A Superclass can have multiple subclasses, however, a subclass may only have 1 superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Object as a superclass

A
  1. In Java, all Objects are subclasses of the class java.lang.Object.
  2. An object is the only class in Java that does not have a superclass.
  3. The only things in the language that are not descendents of java.lang.Object are the primitives: long, int, double, boolean, etc.
  4. Even if no superclass is specified, all classes still implicitly extend from java.lang.Object, and inherit a set of common methods, such as:
    .toString()
    .equals()
    .hashCode()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Casting to a superclass (upcasting)

A
Upcasting is widening, so it is implicit. 
ScientificCalculator sc = new ScientificCalculator();
	Calculator c = sc;
	Object obj = c;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Casting to a subclass (downcasting)

A

Downcasting is narrowing, so must be explicit.
ScientificCalculator sc = new ScientificCalculator();
Calculator c = sc;
ScientificCalculator backToSc = (ScientificCalculator) c;

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