Methods & Encapsulation Flashcards
What two elements of a method signature can never be separated?
The method name and its return type. If they are separated, the code will not compile
What are the 5 requirements for a method signature to be executable by the JVM?
(1) Be called “main”, (2) be public (3) be static (4) Accept an array of strings as a parameter (5) have a return type of void. Note : it can be final
Describe encapsulation
Encapsulation is the concept of writing software in components so that only the necessary parts are exposed for use by client code. e.g. making variables private and only providing access to them via setters and getters. It makes code maintainable (can be changed without changing the API).
Is method overloading determined at compile time or runtime?
Compile time!! Hence, the reference type is used rather than the object type for determining which method is invoked
Describe object creation
Objects can never be created without being constructed. In other words, you cannot have an instance of an object without not only its constructor (or one of its constructors being called) but also all of its superclasses constructors being called.
Whenever you use the new keyword, an objects constructor is called.
Does an abstract class have to have a constructor?
yes - all classes regardless of whether they are abstract must have a constructor even if it is implicit.
Describe constructors and there use
Typically constructors are used to initialize the instance varibales of an object. Avoid using methods that can be overriden.
You should try to have a default empty constructor if it makes sense to do so. It is a design principle - lots of apis make use of classes and require no args constructor.
Describe the call stack for 2 classes A and B, A extends B each having a default constructor, otherwise known as constructor chaining
A is created, then the constructor B is called then Object is called then Object finishes constructing, then B then back down to A.
Describe the default constructor
(1) It has the same access type as the class
(2) It has a no-args constructor
(3) It has a call to the super constructor of the super class
NOTE - every constructor has code added to call super() equivalent if it is not inserted by the programmer
If you supply a no-arg constructor in the super class then you must match that in the subclass. The compiler can only insert a no arg super() call.
Can constructors be inherited?
Nope.
What is the third construct that allows code to be written after methods and constructors?
initialization blocks.
What are the two types of initialization blocks and when are they run?
Static and instance initialization blocks. Static blocks are run when the class if first loaded into the JVM and instance blocks are run each time an instance of a class is created and loaded into the JVM.
The order in which they appear in a class matters. You can have multiple init blocks and they are executed in the order they appear in the class.
Instance blocks are executed immediately after the call to super() in the constructor.
Instance blocks are often used to put code that is shared by all constructors to remove duplication.
What error is thrown in a initialization block?
ExceptionInitializationError regardless of the error. It may then give you more details after throwing this error.
Why do you need static members?
They belong to the class rather than an object of the class. static methods are not dependant in any way upon the state of that particular object.
Can static methods be overriden?
No, they belong to that particular class rather than an instance of the class. They can, however be redefined or shadowed. This is not the same as being overriden, as there are now actually two different methods belonging to the overriden class and the class overriding.