Chapter 5: Class design Flashcards
Methods of an interface always have to be …. and one of the following…
public and either abstract, default or static. if you don’t specify a modifier, they are automatically made public.
Can top level classes be made private?
No, they can only be public or default
what 3 things can be inherited by extending a class?
state - instance fields behaviour - instance methods type - the class its self
what type of methods can’t be overriden by a subclass?
static and private methods can’t be overriden. you can only hide static methods.
What are the rules concerning overriding methods
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.
What modifiers are you not allowed to use on an abstract method?
private, static, final
What kind of variables are interfaces permitted to have?
all variables defined in an interface are implicitly public static & final
Which elements of a class cannot be inherited
constructors, static & instance initialisers aren’t considered members of the class thus are not inherited
What is the difference in the way subclasses inherit instance members vs static members
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
What are the benefits of inheritance?
1) code reuse 2) information hiding 3) polymorphism
Does this code compile if new Animal( ) is called? abstract class Animal{}
No, you can’t instantiate abstract classes.
What is a concrete class?
A class that extends an abstract class
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
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
What happens to child classes as a result of adding an abstract method to the parent
adding an abstract method to the parent class means that the child classes are obligated to provide an implementation for the abstract method.
in what case does a subclass not have to implement its parents abstract method
when the subclass is also declared abstract
Can a final class have an abstract method?
no, final means that a subclass can’t be extended, thus its abstract method would never get implemented which defeats the purpose of abstract
A final method is a …
method that can’t get overriden
which classes can contain final methods?
Any classes whether abstract or final can have final methods.
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(); }
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
Does an abstract class need to have an abstract method?
no
how would you prevent a subclass from hiding the super’s static method?
by using static & final together
What are the rules about the application of access modifiers, final, abstract and static
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
What kind of methods can an interface have?
abstract, static and default methods
what are interface methods by default?
abstract
Can you mark methods of an interface as final?
no
What keyword do you use if an interface wants to inherit from another interface?
extends
What methods of an interface ca’t be inherited?
static methods
can you override a static method with a non static method and vice versa?
no, it will result in compilation error
Can a subinterface override a default method with a static method?
no, it will result in a compilation error
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
}
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
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
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
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?
- super.super.open();
- (Lock)super.open();
- ((Lock)super).open();
- super.open();
- None of the other options
5. None of the other options is correct
- super.super.open() is illegal in java and won’t compile
- (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
- ((Lock)super).open(); illegal and results in compilation error, can’t cast to super keyword
- 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.
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?
- No changes are required as the given code prints LIGHT
- Delete line n1, n2 & n3
- Replace Line n4 with byte…b = 10;
- Replace Line n4 with new Car().speed(byte…) = 10;
§
What are the rules for throwing unchecked/runtime exceptions with regards to inheritance?
If an overridden method throws any unchecked exception or Error, then the overriding method must not throw any checked exceptions.
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
}
}
gogogo is printed
- At Line n1, ‘p’ refers to an instance of class R, thus p.compute(“Go”) invokes the compute(String) method of R class.
- 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”);
- Inside Q, It again invokes the compute(String) method of its Parent class(P) but first changes the string to lowercase. “go”
- Finally P’s method is invoked which changes the sring to “gogogo”
- Control goes back down to compute(String) method of Q and then to the compute(String) method of R, which returns “gogogo”.