Working with Constructors, Methods and Encapsulation Flashcards
True/false: An abstract class can have non abstract methods.
True
True/false: When overriding a method from a super class, you don’t need to consider the access modifier.
False
- an overriding method can’t make the overridden method less accessible.
True/false: You can have as many classes as you want in one java file.
True - but only one class can be public
Which of the following are true about the “default” constructor:
- It is provided by the compiler only if the class and any of its super classes does not define any constructor.
- It takes no arguments.
- A default constructor is used to return a default value.
- To define a default constructor, you must use the default keyword.
- It is always public.
2
- A constructor does not return any value at all and The access type of a default constructor is same as the access type of the class.
True/false: An overloaded method means a method with the same name and same number and type of arguments exists in the super class and sub class.
False.
- This is called an overridden method
True/false: Static methods don’t have the “this” - this includes static void main()
True
True/false: The following class declaration is valid, assuming two interfaces J and I exists: abstract class MyIJ implements J , I { }
True
Which of the following statements are true:
- private keyword can never be applied to a class.
- synchronized keyword can never be applied to a class.
- synchronized keyword may be applied to a non-primitive variable.
- final keyword can never be applied to a class.
- A final variable can be shadowed in a subclass.
2, 5
- private, protected and public can be applied to a nested class.
- Synchronized can only be applied to a method or a block.
- final can be applied to class, variable and methods.
Which of these statements are true:
- All classes must explicitly define a constructor.
- A constructor can be declared private.
- A constructor can declare a return value.
- A constructor must initialize all the member variables of a class.
- A constructor can access the non-static members of a class.
2, 5
- private is used for implementing Singleton Classes (2).
- All non-final instance variables get default values if not explicitly initialized (4).
- A constructor is non-static, and so it can access directly both the static and non-static members of the class (5).
Which of the following are valid:
- public TestClass(int a, int b) { }
- public void TestClass(int a) { }
- public TestClass(String s);
- private TestClass(String s, int a) { }
- public TestClass(String s1, String s2) { };
1, 4, 5
- Constructors cannot have empty bodies (i.e. they cannot be abstract) (3)
- You can apply public, private, protected to a constructor. But not static, final, synchronized, native and abstract. (4)
- The compiler ignores the extra semi-colon (5).