Class Design Flashcards

1
Q

What is included when a class inherits from another?

A

The public and protected primitives, objects, or methods defined in the parent class

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

Single Inheritance

A

Java only allows a class to have one direct parent class

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

How can you keep a class from being extended?

A

Mark it with the final modifier

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

What is the basic Object class?

A

java.lang.Object

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

How do you call the parent class’s constructor?

A

super()

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

What type of constructor must a parent class have if you don’t specify a certain constructor in the child class to make things work?

A

A default no-arguments constructor

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

What are the rules for overriding a method in a child class?

A

1) The method in the child class must have the same exact signature (name and parameters) as the one in the parent class
2) The method in the child class must be at least as accessible as the one in the parent class
3) The method in the child class may not throw a checked exception that is new or broader than in the parent method
4) Return type of the child class method must be the same or a subclass of the return type in the parent class

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

Hidden Method

A

When a child class defines a static method with the same name and signature as a static method defined in the parent class.

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

Can you override or hide variables?

A

You can only hide them

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

Abstract Class

A

A class that is marked with the abstract keyword and cannot be instantiated.

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

Abstract Method

A
A method marked with the abstract keyword defined in an abstract class, for which no implementation is provided in the class in which it is declared.
Can only be declared in an abstract class.

public abstract String() getName();

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

Concrete Class

A

The first non-abstract subclass that extends an abstract class. It is required to implement all inherited abstract methods, which have not been implemented in a parent class.

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

Can an abstract class that extends an abstract class provide an implementation for methods inherited?

A

yes! but they don’t have to

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

Abstract Class Definition Rules

A

1) Abstract classes cannot be instantiated directly.
2) Abstract classes may be defined with any number, including zero, of abstract and nonabstract methods.
3) Abstract classes may not be marked as private or final.
4) An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
5) The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.

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

Abstract Method Definition Rules

A

1) Abstract methods may only be defined in abstract classes.
2) Abstract methods may not be declared private or final.
3) Abstract methods must not provide a method body/implementation in the abstract class for which is it declared.
4) Implementing an abstract method in a subclass follows the same rules for overriding a method. For example, the name and signature must be the same, and the visibility of the method in the subclass must be at least as accessible as the method in the parent class.

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

Interface

A

An abstract data type that defines a list of abstract public methods that any class implementing the interface must provide. An interface can also include a list of constant variables and default methods.

17
Q

How to declare and interface

A

public abstract interface myInterface {

}

18
Q

How to say you want to use an interface

A
public class myClass implements myInterface { 
}
19
Q

Rules of interfaces

A

1) Interfaces cannot be instantiated directly.
2) An interface is not required to have any methods.
3) An interface may not be marked as final.
4) All top-level interfaces are assumed to have public or default access, and they must include the abstract modifier in their definition. Therefore, marking an interface as private, protected, or final will trigger a compiler error, since this is incompatible with these assumptions.
5) All nondefault methods in an interface are assumed to have the modifiers abstract and public in their definition. Therefore, marking a method as private, protected, or final will trigger compiler errors as these are incompatible with the abstract and public keywords.

20
Q

How many interfaces can an interface extend?

A

As many as it wants!

21
Q

How many interfaces can a class implement?

A

As many as it wants!

22
Q

What are interface variables assumed to be?

A

public, static, final

23
Q

Default Method

A

A method in an interface that has a default implementation specified rather than just being abstract. Note that it can still be overridden as if it were abstract.
Denoted with the “default” keyword

24
Q

Static Methods in Interfaces

A

Aren’t inherited, so must be referenced as interfaceName.methodName();

25
Q

Polymorphism

A

A java object can take the form of it’s default class, and also anything that class extends or implements

26
Q

Casting an object from a ______ to a _______ doesn’t require an explicit cast.
Casting an object from a _______ to a _______ requires an explicit cast.

A

subclass, superclass

superclass, subclass

27
Q

Virtual Method

A

A method whose precise implementation is not determined until runtime. All nonfinal, nonprivate methods are virtual methods in Java since they may be overridden at runtime.

28
Q

Polymorphic Parameters

A

The property of method parameters in Java used to accept sub- classes and subtypes of the method parameter type automatically.

29
Q

What is always run as the first line in a constructor?

A

If there isn’t an explicit call to a different constructor using a variation of this() or super(), the compiler will attempt to insert a no argument super() call. However, if the parent class doesn’t have a no argument constructor, you MUST explicitly write in a line that works with a variation of this() or super()

30
Q

Difference between hidden and overridden methods

A

When a method is hidden, if the method is called from the parent class then it uses the definition in the parent class, and if the method is called from the child class then it uses the definition from the child class

When a method is overridden, whether the method is called from the parent class or the child class, it will use the definition in the child class.