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