Chapter 5: Class design Flashcards

1
Q

Methods of an interface always have to be …. and one of the following…

A

public and either abstract, default or static. if you don’t specify a modifier, they are automatically made public.

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

Can top level classes be made private?

A

No, they can only be public or default

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

what 3 things can be inherited by extending a class?

A

state - instance fields behaviour - instance methods type - the class its self

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

what type of methods can’t be overriden by a subclass?

A

static and private methods can’t be overriden. you can only hide static methods.

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

What are the rules concerning overriding methods

A

To override a method correctly: 1) the overriding method must have the same signature 2) the return type has to either be the same or a subtype 3) overriding method can’t be less accessible than overriden method. 4)The overriding method can’t declare more exceptions than declared by the overriden method unless the exceptions are a subtype or are runtime exceptions as it doesn’t matter how many of those you declare.

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

What modifiers are you not allowed to use on an abstract method?

A

private, static, final

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

What kind of variables are interfaces permitted to have?

A

all variables defined in an interface are implicitly public static & final

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

Which elements of a class cannot be inherited

A

constructors, static & instance initialisers aren’t considered members of the class thus are not inherited

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

What is the difference in the way subclasses inherit instance members vs static members

A

subclasses can inherit their own copy of instance members this is shown by overriding and how subclasses can replace the behaviour of the methods with their own implementation. However subclasses only get access rights static members and can’t not change their behaviour, only hide it

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

What are the benefits of inheritance?

A

1) code reuse 2) information hiding 3) polymorphism

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

Does this code compile if new Animal( ) is called? abstract class Animal{}

A

No, you can’t instantiate abstract classes.

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

What is a concrete class?

A

A class that extends an abstract class

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

Which of the following are valid declarations inside an interface independent of each other? void compute(); // 1 public void compute(); // 2 public final void compute(); // 3 static void compute(); // 4 protected void compute(); // 5

A

1) All interface methods have to be public. No access control keyword in the method declaration also means public in an interface. 2) correct 3) final is not allowed. 4)An interface can have a static method but the method must have a body in that case. 5) all methods must be public

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

What happens to child classes as a result of adding an abstract method to the parent

A

adding an abstract method to the parent class means that the child classes are obligated to provide an implementation for the abstract method.

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

in what case does a subclass not have to implement its parents abstract method

A

when the subclass is also declared abstract

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

Can a final class have an abstract method?

A

no, final means that a subclass can’t be extended, thus its abstract method would never get implemented which defeats the purpose of abstract

17
Q

A final method is a …

A

method that can’t get overriden

18
Q

which classes can contain final methods?

A

Any classes whether abstract or final can have final methods.

19
Q

Will this code compile? class Sofa { private final void recline(){ } // 1 } abstract class Sofa { private abstract void recline(); // 2 } abstract class Bed extends Furniture{ static abstract void getWidth(); }

A

1) yes! you can have a mark a final method as private, its a bit redundant as they basically mean the same thing 2) doesn’t compile, a private member can’t be inherited 3) static methods can’t be overriden thus this will not compile

20
Q

Does an abstract class need to have an abstract method?

A

no

21
Q

how would you prevent a subclass from hiding the super’s static method?

A

by using static & final together

22
Q

What are the rules about the application of access modifiers, final, abstract and static

A

1)An abstract class doesn’t necessarily have to have an abstract method but if a class has an abstract method, it must be declared abstract. 2)A final class or a final method cannot be abstract. 3)A final class cannot contain an abstract method but an abstract class may contain a final method . 4)A private method is always final . 5)A private method can never be abstract . 6)A static method can be final but can never be abstract

23
Q

What kind of methods can an interface have?

A

abstract, static and default methods

24
Q

what are interface methods by default?

A

abstract

25
Q

Can you mark methods of an interface as final?

A

no

26
Q

What keyword do you use if an interface wants to inherit from another interface?

A

extends

27
Q

What methods of an interface ca’t be inherited?

A

static methods

28
Q

can you override a static method with a non static method and vice versa?

A

no, it will result in compilation error

29
Q

Can a subinterface override a default method with a static method?

A

no, it will result in a compilation error

30
Q

Is this legal?

interface I {

public default void invalid(){ }

public static void valid(){ } // 1

}

interface I2 extends I{

public static void invalid(){ } //2

public default void valid(){ } //3

}

A

1) Can be called only using I.valid(); 2) will not compile 3) this is legal and will compile you can have a default method in a subinterface with the same signature as a static method in the super

31
Q

Complete the code with the correct option below: public class Exam { void method(){} } public class OCAJP extends Exam{ …………. void method(){ } } A. abstract B. final C. private D. default E. int

A

B is the correct answer Explanation: B. we can use final since the method is not abstract A. can’t use abstract keyword with non-abstract method ( also abstract method has no body) C. overriding method use a more restrictive modifier than parent method D. default methods are only allowed in interfaces

32
Q

Given code of Test.java file:

package com.udayankhattry.oca;

class Lock {

public void open() {

System.out.println(“LOCK-OPEN”);

}

}

class Padlock extends Lock {

public void open() {

System.out.println(“PADLOCK-OPEN”);

}

}

class DigitalPadlock extends Padlock {

public void open() {

/*INSERT*/

}

}

public class Test {

public static void main(String[] args) {

Lock lock = new DigitalPadlock();

lock.open();

}

}

Which of the following options, if used to replace /*INSERT*/, will compile successfully and on execution will print LOCK-OPEN on to the console?

  1. super.super.open();
  2. (Lock)super.open();
  3. ((Lock)super).open();
  4. super.open();
  5. None of the other options
A

5. None of the other options is correct

  1. super.super.open() is illegal in java and won’t compile
  2. (Lock)super.open() => compilation error, super.open() is evaluated first as (.) has higher precedence than cast. The call returns void and can’t be casted to Lock as they are incompatible types
  3. ((Lock)super).open(); illegal and results in compilation error, can’t cast to super keyword
  4. this option will case Padlocks method to be invoked instead of Digital padlock

its not possibe to directly access 2 levels above the child. super allows access to 1 level above.

33
Q

Given code of Test.java file:

package com.udayankhattry.oca;

class Car {

void speed(Byte val) { //Line n1

System.out.println(“DARK”); //Line n2

} //Line n3

void speed(byte… vals) {

System.out.println(“LIGHT”);

}

}

public class Test {

public static void main(String[] args) {

byte b = 10; //Line n4

new Car().speed(b); //Line n5

}

}

Which of the following needs to be done so that LIGHT is printed on to the console?

  1. No changes are required as the given code prints LIGHT
  2. Delete line n1, n2 & n3
  3. Replace Line n4 with byte…b = 10;
  4. Replace Line n4 with new Car().speed(byte…) = 10;
A

§

34
Q

What are the rules for throwing unchecked/runtime exceptions with regards to inheritance?

A

If an overridden method throws any unchecked exception or Error, then the overriding method must not throw any checked exceptions.

35
Q

Consider below code of Test.java file, what is printed?

public class Test {

public static void main(String[] args) {

P p = new R(); //Line 1

System.out.println(p.compute(“Go”)); //Line 2

}

}

class P {

String compute(String str) {

return str + str + str;

}

}

class Q extends P {

String compute(String str) {

return super.compute(str.toLowerCase());

}

}

class R extends Q {

String compute(String str) {

return super.compute(str.replace(‘o’, ‘O’)); //2nd argument is uppercase O

}

}

A

gogogo is printed

  1. At Line n1, ‘p’ refers to an instance of class R, thus p.compute(“Go”) invokes the compute(String) method of R class.
  2. inside this method, there is a call to super(class Q), super.compute(str.replace(‘o’, ‘O’)); which replaces o with O and calls super passing in the sting “GO” –> super.compute(“GO”);
  3. Inside Q, It again invokes the compute(String) method of its Parent class(P) but first changes the string to lowercase. “go”
  4. Finally P’s method is invoked which changes the sring to “gogogo”
  5. Control goes back down to compute(String) method of Q and then to the compute(String) method of R, which returns “gogogo”.