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