OOP Flashcards
Is the constructor inherited?
No, the constructor is not inherited.
Can you make the constructor final?
No, the constructor cannot be final. In fact constructors can only take access modifiers. They cannot be abstract, final, native, static, or synchronized.
T/F: You can use the keyword “this” anywhere in a constructor.
False. The keyword “this” must be used on the first line. The keyword “super” has this rule as well.
T/F: The Java compiler will add a call to super() in your constructor if you do not have a call to super.
TRUE! super() is automatically added if missing. The call to super must be the first line in the constructor. Also if the super class does not have a no-argument constructor, you will get a compile-time error.
Can you execute a program without a main() method?
Yes, but you must use a version of the JDK older than 1.7 and use a static block. Starting with JDK 1.7 you will get an error.
What is the difference between aggregation and composition?
In aggregation, the child objects have their own lifecycle and can exist without the parent. In composition, the child objects do have their own lifecycle and should NOT exist without their parent.
What is the difference between an argument vs a parameter?
“Parameters” refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked.
Can you use this() and super() both in a constructor?
No, because both statements MUST be the FIRST statement in the constructor.
Can you overload the main() method?
Yes. But but when running a class from the command line, java will select the main() method with the standard signature.
Can you override a static method?
No. Static methods are part of the class, not the object instance.
What is a covariant return type?
Starting with Java 1.5, you can override a method by changing the return type so that it is a subclass of the original method’s return type. E.g. the overriden method could return a Circle instead of a Shape. Before Java 1.5, the method signature had match arguments and return type exactly.
Can you instantiate an abstract class?
No, abstract classes cannot be instantiated, they can only be extended.
What is a marker interface?
A marker interface is an interface that has no data members or methods.
Can an abstract class have a constructor?
Yes