Working with Inheritance Flashcards
Consider the following interface: public interface IInt{ int thevalue = 0; } True/false: any class that implements the this interface inherits this field. Therefore, it can be accessed using s.thevalue or just 'thevalue' inside the class.
True:
- Also, since it is static, it can also be accessed using IInt.thevalue or “implementing class”.thevalue.
True/false: As a rule, fields defined in an interface are public, static, and final. (The methods are public and abstract.)
True
True/false: static method cannot be overridden by a non-static method and vice versa
True
True/false: a static method cannot be shadowed by a static method in the subclass.
False
- Static methods are shadowed like static variables and therefore can’t be overridden
True/false: Fields defined in an interface are ALWAYS considered as public, static, and final. (Methods are public and abstract.)
True
- In fact, you cannot even declare a field to be private or protected in an interface.
Which one of these is a proper definition of a class Car that cannot be sub-classed:
- class Car { }
- abstract class Car { }
- native class Car { }
- static class Car { }
- final class Car { }
5
- Notice the distinction. For classes, final means it cannot be extended, while for methods, final means it cannot be overridden in a subclass.
The native keyword can only be used on methods, not on classes and instance variables.
True/false: Fields in an interface are implicitly public, static and final.
True
- Although you can put these words in the interface definition but it is not a good practice to do so.
Which of these statements about interfaces are true:
1. Interfaces are abstract by default.
2. An interface can have static methods.
3. All methods in an interface are abstract although you need not declare them to be so.
4, Fields of an interface may be declared as transient or volatile but not synchronized.
5. interfaces cannot be final.
1, 3, 5
Which of the following statements are correct:
- An abstract class can be extended by an abstract or a concrete class.
- A concrete class can be extended by an abstract or a concrete class.
- An interface can be extended by another interface.
- An interface can be extended by an abstract class.
- An interface can be extended by a concrete class.
- An abstract class cannot implement an interface.
1, 2, 3
True/false: if(b instanceof Bird) will fail to compile if b is not a subclass of bird or any of birds subclasses.
True
True/false: a*10/4-30; generates an int which can be returned as a double without any cast.
True
True/false: The return type should always be the same for overridden and overriding method.
False - This is true for primitives, but for Objects the overriding method can take the same or any subclass as return type.
True/false: A super( ) or this( ) call must always be provided explicitly as the first statement in the body of the constructor.
False
True/false: If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).
True
True/false: super(…) can only be called in the first line of the constructor but this(…) can be called from anywhere.
False