Chapter 5 - Class Design Flashcards

1
Q

What are the rules for a concrete class to extend an abstract class?

A
  1. The concrete class should implement all the methods from the abstract class.

Abstract classes extending abstract classes do not need to implemet the methods from the class that is being extended.

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

Can an abstract class be instantiated directly?

A

False

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

Do abstract classes require abstract and no abstract methods to be defined?

A

No. Abstract classes may define any number or zero abstract or non-abstract methods.

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

Can abstract classes may be marked as private, protected or final?

A

No. By any means abstract classes can be marked as private, protected or final.

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

Does an abstract class that extends an abstract class inherits all of its abstract methods as is own abstract methods?

A

True

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

Does the first concrete class that extends an abstract class must provide an implementation for all the inherited abstract methods?

A

True

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

Must abstract methods only be defined in abstract classes?

A

Yes. Abstract methods cannot be defined in classes that are not abstract.

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

Can abstract methods be declared private or final?

A

No. Abstract methods cannot be of the type private or final.

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

What are the rules for implementing an abstract method?

A
  1. The name and signature of the implemented method should be the same.
  2. 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
10
Q

What an interface is?

A

An interface is a specialized kind of abstract class. It shares many of the same properties and rules as an abstract class.

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

Can an interface be intanciated directly?

A

No. This is the interfaces rule # 1. Interfaces as abstract classes cannot be directly instantiated.

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

Must interfaces define methods?

A

No. An interface is not required to define methods.

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

Can an interface be defined as final?

A

No. This is interfaces rule #3. An interface may not be marked as final.

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

Must interfaces have the abstract keyword in them?

A

No. Top level interfaces are asume to be abstract whether the “abstract” keyword id present or not.

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

What will happen if an interface method is marked as private, protected or final?

A

A compiler error will be emitted. All top level interfaces are assume to be abstract whether the keyword is present or not. Therefore it is not possible to make a method private, protected or final.

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

Can a nondefault method in an interface be marked as private, protected or final?

A

No. This will trigger a compiler error. All nondefault methods in an interface are assume to have the modifiers abstract and public in their definition. So, defining methods as private, protected or final should not be done.

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

What is the inheriting an interface rule # 1?

A

An interface that extends another interface, as well as an abstract class that implements an interface, inherits all the abstract methods as its own abstract methods.

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

What is the inheriting an interface rule # 2?

A

The first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all the inherited abstract methods.

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

Can an abstract class extend several abstract classes at the same time?

A

No. Just one abstract class can be extended at a time.

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

Can multiple interfaces be extended at the same time?

A

Yes. An interface may extend multiple interfaces.

Example:

public interface Seal extends HasTail, HasWhiskers{ }

Where HasTail and HasWiskers are interfaces.

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

Do abstract classes must include ‘abstract’ keyword in their definition?

A

Yes, abstract classes must include abstract keyword.

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

Do interfaces must include ‘abstract’ keyword in their definition?

A

No, they may or not include ‘abstract’ in their definition. If not included, Java includes it by default.

23
Q

Can a class extend an interface?

A

No. A class can only implement an interface.

24
Q

Can an interface implement another interface?

A

No. An interface can only extend another interface.

25
Q

Can a interface extend a class?

A

No. An interface cannot extend a class. This generates a compiler error.

26
Q

Can 2 methods in a class be defined with the same name and input parameters but different return type?

A

No. The code won’t compile.

27
Q

What is rule # 1 for Interface Variables?

A

Interface variables are assumed to be public, static and final.

28
Q

Can interface variables be private or protected?

A

No. This will trigger a compiler error.

29
Q

What is rule # 2 for Interface Variables?

A

The value of an interface variable must be set when it is declared since it is marked as final.

Interface variables are essentially constant variables defined on the instance level. Because they are assumed to be static, they are accessible even without an instance of the interface.

30
Q

Can default interface methods be declared within a class or abstract class?

A

No. A default method may only be declared within an interface.

31
Q

Must default methods have the ‘default’ keyword in them?

A

Yes. Default methods must be marked with the default keyword and must provide a method body.

32
Q

Can a default method be marked as static, final or abstract?

A

No. A default method is not assumed to be static, final or abstract because it may be used or overridden by a class that implements the interface.

33
Q

Can a default method be marked as private or protected?

A

No. Like all methods in interfaces, a default method is assumed to be public and will not compile if marked as private or protected.

34
Q

Can interface methods with a body be declared without the default keyword?

A

No. This will generate a compiler error. Interfaces may only contain methods with a body that are marked as default.

35
Q

Can a class implement 2 interfaces that have the default methods with the same name and signature?

Example:
public interface Walk{
  public default int getSpeed( ) {
      return 5;
     }
  }
public class Cat implements Walk, Run { 
  public static void main (String[] args)
  system.out.println(new Cat().getSpeed());
 }

}

A

No. That will generate a compiler error because Java won’t know which method to choose. To fix this issue the subclass can override the duplicate default method.

36
Q

What is the default access modifier for an Static Interface Method?

A

A static method in an interface is assumed to be public and will not compile if marked as private or protected.

37
Q

Can a static method defined in an interface be inherited by a class that implements the interface?

A

No. Static methods defined in an interface are not inherited in any class that implements the interface.

38
Q

What needs to be done to reference a static method?

A

A reference to the name of the interface the static method belongs to should be used.

39
Q

In lambda expressios, can variables be redeclared?

A

No. In lambda expressions variables cannot be redeclared.

40
Q

What the type of the object determines?

A

The type of the the object determines which properties exist within the object in memmory.

41
Q

What the reference type to the object determine?

A

The type of reference to the object determines which methods and variables are accesible.

42
Q

What is a Virtual Method?

A

A Virtual Method is a method in which the specific implementation is not determined until runtime.

43
Q

In Virtual Methods, what do you get if you call a method on an object that overrides a method? The parent o or the overriden method?

A

You get the overriden method, even if the call to the method is on a parent reference or within the parent class.

44
Q

What is the rule related with accessibility for overriden methods?

A

Overriden methods must be at least as accessible as the method it is overriding, otherwise the code won’t compile.

45
Q

What is the rule related with exceptions in polrmorphims and methods overriding?

A

A subclass cannot declared an overriden method with a new or broader exception.

46
Q

What rule is related with return types in polymorphims and method overriding?

A

Overriden methods may use covariant return types.

47
Q

What is one special rule for constructors and overriding methods that needs to be followed?

A

If the parent class does not include a non-argument constructor, an explicit call to a parent constructor must be provided in the child’s contructor.

48
Q

In a static method, should a body be provided?

A

Yes, in static methods a body needs to be provided, otherwise a compiler error will be thrown.

49
Q

Should an interface method that provides a body be marked as default or static explicitly?

A

Yes. An interface method that provides a body must be marked as static or default, otherwise a compiler error will be thrown.

50
Q

Can methods mark as final be overriden?

A

No. Methods marked as final cannot be overriden.

51
Q

Abstract classes methods should be implemented by concrete classes extending the abstract class. Should the methods be overriden or overloaded?

A

Abstract methods inherited by a concrete class must be overriden, not overloaded.

52
Q

Can null value always be passed as an object value?

A

Yes, null can be passed as an object value regardless of type.

53
Q

Can variables be overriden?

A

No. Variables can only be hidden. They cannot be overriden even if they are public.

54
Q

Which methods can be overriden? With which type of access modifiers?

A

Methods with access modifier public and protected can be overriden. Static or private methods can NOT be overriden.