Abstract Classes Flashcards

1
Q

For every Java component family, do all of the classes of that component family contain code for all methods that are introduced in/inherited by the interface(s) implemented by those classes?

A

No!

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

Name the ubiquitous class that “every” class in Java extends and define it.

A

Object - it is a special built-in class that provides default implementations for 3 instance methods: boolean equals(Object obj), int hashCode(), and String toString()

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

What does hashCode() return?

A

hashCode() returns the result of some fixed integer-valued function applied to this.

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

Why do we (almost always) need to override every method implemented by the Object class?
(override - allowance of a child/sub-class to to provide a specific implementation of a method that is already provided by one of its parent/super-class. When a method in a subclass has the same name, the same parameters or signature, and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

A

The default implementation of a certain instance method by Object class returns a value of the same type as the method. This value depends on the reference value of “this” instead of the object value.

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

Abstract class

A

A “partial”/”incomplete” class that contains implementation bodies for some but not all of the methods of the interfaces it claims to implement.

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

Why can you not instantiate an abstract class in Java?

A

Some methods of the interface directly implemented by the abstract class still might not have implementation bodies/code in that abstract class;
For example NaturalNumberSecondary is an abstract class that implements the NaturalNumber interface, which extends NaturalNumberKernel which in turn extends Standard interface. NaturalNumberSecondary contains implementation bodies/code ONLY for the 14 methods INTRODUCED in NaturalNumber interface (i.e. add, subtract, increment, multiply, decrement, copyFrom, etc.) but no code for methods introduced in NaturalNumberKernel (multiplyBy10, divideBy10, isZero) or Standard (clear, newInstance, transferFrom). Hence, NaturalNumberSecondary cannot be instantiated.

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

What is the best purpose of an abstract class?

A

An abstract class can be used to implement code for and store bodies of methods that can be written once and work for any implementation of the kernel interface because such methods are implemented using the kernel methods (methods defined in the kernel interface), and hence are programmed into the kernel interface.

The remaining constructors and kernel methods can be implemented by kernel classes

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