37 terms

OCA Java SE 7: Inheritance

Working with Inheritance * Implement inheritance * Develop code that demonstrates the use of polymorphism * Differentiate between the type of a reference and the type of an object * Determine when casting is necessary * Use super and this to access objects and constructors * Use abstract classes and interfaces
STUDY
PLAY
True or False: A class can not implement multiple interfaces.
False.
True or False: An interface can inherit zero or more interfaces. An interface cannot inherit a class.
True.
True or False: class that inherits another class is called a derived class or subclass.
True.
True or False: A class that is inherited is called a parent or base class.
True.
True or False: Private members of a base class can be inherited in the derived class.
False.
True or False: A derived class can only inherit members with the default access modifier if both the base class and the derived class are in the same package.
True.
True or False: A class uses the keyword extends to inherit a class.
True.
True or False: An interface uses the keyword extends to inherit another interface.
True.
True or False: A class uses the keyword extends to implement an interface.
False. It uses the keyword implements to implement an interface.
True or False: An interface can extend multiple interfaces.
True.
True or False: The method signatures of a method defined in an interface and in the class that implements the interface must match; otherwise, the class won't compile.
True.
True or False: With inheritance, you can also refer to an object of a derived class using a variable of a base class or interface.
True.
True or False: An object of a base class can be referred to using a reference variable of its derived class.
False.
True or False: When an object is referred to by a reference variable of a base class, the reference variable can only access the variables and members that are defined in the base class.
True.
True or False: When an object is referred to by a reference variable of an interface implemented by a class, the reference variable can access only the variables and methods defined in the interface.
True.
Question: If the class Manager extends the class Employee, and a reference variable emp of type Employee is used to refer to an object of the class Manager, what operation can be done to access the derived class variables and methods of Manager?
Perform a cast operation: ((Manager)emp)
True or False: If a method defines a local variable or method parameter with the same name as an instance variable, the keyword this must be used to access the instance variable in the method.
True.
What are the conditions for a method to be polymorphic?
The polymorphic methods are also called overridden methods. Overridden methods should define methods with the same name, same argument list, same list of method parameters. The return type of the overriding method can be the same, or a subclass of the return type of the overridden method in the base class, which is also known as covariant return type. Access modifiers for an overriding method can be the same or less restrictive but can't be more restrictive than the method being overridden.
What conditions must a method meet to be an overridden one?
A derived class is said to override a method in the base class if it defines a method with the same name, same parameter list, and same return type as in the derived class.
True or False: If a method defined in a base class is overloaded in the derived classes, then these two methods (in the base class and the derived class) are called polymorphic methods.
False. Only overridden methods can be called polymorphic.
True or False: When implementing polymorphism with classes, a method defined in the base class may or may not be abstract.
True.
True or False: When implementing polymorphism with interfaces, a method defined in the base interface is always abstract.
True.
Explain "Reference Type" and "Object Type"
The reference type corresponds to the type in a variable declaration. The object type corresponds to the instantiated class in an object variable declaration. So "reference type" is the LHS type and "object type" is the actual instantiated type on the RHS of an object variable declaration.
True or False: The super keyword can be used to invoke super class methods in the subclass's overloaded methods?
True.
True or False: This statement will compile "( (Object)newString.toString("Foo") )" where class NewString has overloaded method "String toString(String arg)".
False. The cast to Object will fail to compile since the Object class does not have a toString(String) method.
True or False: The reference type determines which members of a variable object are accessible
True. Only those members allowing access according to their access modifiers will be available to be called. Furthermore, only those available methods are candidates for polymorphic calls.
True or False: A class with a private constructor can be inherited.
False.
True or False: The invocation of the base class' default constructor occurs automatically unless we use the super keyword to invoke an alternative base class constructor.
True.
Can a subclass override a method and declare fewer thrown exceptions that the superclass method?
Yes, it is legal. The overriding method does not have to declare "throws" on checked exceptions declared by the super class.
For overloaded methods, is it the reference type or dynamic runtime object type that determinesl which method gets called?
The reference type (not the object type) determines which overloaded method is invoked!
True or False: constructors are inherited by the subclass.
False. Never.
What is the order of execution of instance initializers and constructors-in base and subclasses? Constructors first or initializers?
Each class will have its initializers called first, before the constructor. The super class constructor will be called first, implicitly or excplicitly, before the subclass.
Can instance initializers be inherited by a derived class?
No. The instance initializers can't be defined with any explicit access. They can only be defined using the 'default' access. Also, instance initializers aren't inherited by derived class. They execute when a class is instantiated.
What is the access modifier for the default constructor?
The default constructor is the no-arg one provided by the JVM. It will be assigned the same access modifier as the specified for the class itself.
When can a child class in another package access a protected member in the parent class?
Only when the children tries to access it own variable, not variable of other instance
True or False: An abstract method can not be overridden.
False. Abstract methods are meant to be overridden by subclasses.
When are static instance blocks executed in an objects lifecycle?
Immediately after the call to super's constructor and before the instance constructor's code. Blocks are called in top to bottom order.
YOU MIGHT ALSO LIKE...