Level 109 Level 111
Level 110

[Quiz] Inheritance II

125 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Type the main method:
public static void main(String args[]){}
main, main, methods
In the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's _______() method. The _______() method then calls all the other _______required to run your application.
class, class, implement, interface
reference, Object
Wolf aWolf = new Wolf();
public , private
Asubclass Inherits all public/private instance
instance vartables, redefined
Inherited methods can be overridden; _______ cannot be overridden (although they
reference, reference, Dog, Dog
Dog myDog = new Dog () ;
new Dog ();
Dog myDog = new Dog () ;
Dog myDog = new Dog () ;
Dog myDog = new Dog () ;
Intantantiating a class.
Animal myDog = new Dog();
With polymorphism, the reference
Polymorphism lets you do
Animal[] animals = new Animal[5];
Dog is a subclass of Animal.
constant variables are defined with the word:
//..superclass code
No, method1(Int variable);
Indicates that the details of a class, a method, or an interface are given elsewhere in the code.
instance, polymorphism
An abstract class means that nobody can ever make a new _______ of that class. You can still make that abstract class as a declared reference type, for the puposes of _______.
abstract public class Canine extends Animal
An abstract class has virtually* no use, no value, no purpose in life, unless it is _______
instances, subclass
With an abstract class, the guys doing the work at runtime are _______of a _______ of your abstract class.
A class that is nog abstract is called a _______ class.
public Polygon polygonFrom(Point... corners) {
Object o = new TestClass();
Object o = new TestClass();
More efficient way to use a tree to implement a priority queue.
many forms
'Polymorphism' means
Dog rerVarDog = (Dog) refVarObject;
Cast the reference variable "refVarObject" of type Object to type Dog and store it in the variable rerVarDog that is of type Dog.
Object o = new TestClass(){
This is a instance of Testclass
if (myDog instanceof Dog) {}
Write a line of code so you can check if refence variable myDog is an instance of the class Dog.
public interface Pet(){
define public interface Pet(){
Use an interface when you want to define a _______
Object class.
All classes in Java must have exactly have one base class, the only exception being the _______ class.
type, prototype, interface
Other terms for interface are t_______, p_______ and i_______
True or False: The enhanced for loop can be used to initialize an array and modify its elements.
In Java, Exceptions are objects. True or false?
False. It uses the keyword implements to implement an interface.
True or False: A class uses the keyword extends to implement an interface.
Perform a cast operation: ((Manager)emp)
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 ac…
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…
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.
False. Only overridden methods can be called polymorphic.
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.
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…
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.
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.
The reference type (not the object type) determines which overloaded method is invoked!
For overloaded methods, is it the reference type or dynamic runtime object type that determinesl which method gets called?
False. Never.
True or False: constructors are inherited by the subclass.
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.
Only when the children tries to access it own variable, not variable of other instance
When can a child class in another package access a protected member in the parent class?
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.
the class Obect
In Java, every class that does not specifically extend another class extends this
Denotes inheritance
solid line with hollow triangle tip
behavior and state
The subclass inherits these from the superclass
code reuse
One important reason got inheritance
new methods and instance fields
must be defined in the subclass
inheritance hierarchies
sets of classes can form complex
override, inherit, define new
things you can do to methods in a subclass
overriding a method
if you specify a method with the same signature (same name and same parameter types) it overrides the method of the same name in the superclass
inheriting methods
if you do not explicitly override a superclass method you automatically inherit it; the superclass methods can be applied to the subclass obects
the new method can only be applied to subclass obects
If you define a method that does not exist in the superclass then,
instance fields
You can never override
Subclasses inherit all
fields from the superclass
subclass objects
Any new instance fields that you define in the subclass are present only in
private fields of its superclass
a subclass has no access to
infinite recursion
failing to invoke the superclass method results in
calling a superclass constructor
use the super keyword in the first statement of the subclass constructor
subclass references
can be converted into superclass references
method calls are always determined by
the type of the actual object (it doesn't matter if the object reference is stored in a different field type)
abstract class
A class that contains one or more abstract methods, and therefore can never be instantiated. Abstract classes are defined so that other classes can extend them and make them concrete by implementing the abstract methods.
concrete class
a class which you can create objects for
how do abstract classes differ from interfaces
abstract classes can have instance fields and they can have concrete methods and constructors
makes it so that no one can extend the class
declaring the class final (you can also declare methods as final so that they can't be overridden)
four levels of controlling access to fields
public access, private access, protected access, package access
package access
the default access when no access modifier is given
private features
can only be accessed by the methods of their own class
public features
can be accessed by methods of all classes
protected features can be accessed by
all subclasses and all classes in the same package
define the toString method to
yield a string that describes the object state
return getClass().getName() + desired print info
If you want your toString method to be usable by a subclass
equals method
tests whether two objects have the same contents
== method
tests whether two references are to the same object
how to wrtie equals method
cast the otherObject parameter to the type of your class, and then compare the fields of the implicit parameter and the other parameter
if (otherObject == null) return false;
testing whether two objects belong to the same class
when you call the clone method you must use a
enumerated type (enum)
a type with a finite number of values
in enumeration classes, the toString method returns
a string that equals the objects name
in enumeration classes, the clone method returns
the given object WITHOUT making a copy
subclass "extends" superclass
reserved word used in inheritance programs
inheritance implements the "is-a" relationship between objects
a high school is a type of school
grad student --> student --> person
the arrow points the subclass to its superclass, denoting the is-a relationship
super. prefix
use this to make an explicit call to a superclass method from a subclass
constructors are never inherited
a subclass must provide one on its own
if no constructor is written into the subclass....
a default constructor with no parameters is generated; 0 or null
a compiler error will occur
if the superclass only has constructor with params and no default constructor...
polymorphism(poly - many, morph - types)
a side effect of inheritance; an object variable may refer to objects of its own class and objects from any of its subclasses(compiler doesnt know whether solid is sphere or cube; so it change…
dynamic method binding
each object holds a pointer to a table allowing the proper method to be called at run-time
abstract(no code/some info = abstract)
classes close to the top of the hierarchy are more _______
superclass and the method abstract
if a superclass has a method that is unique for each subclass, consider making the....
variables or arrays of its type
you cannot instanciate an abstract class, but you can declare...
concrete classes
classes where all methods are defined; found at the bottom of the hierarchy
inter = between
intra = within
the actual code
interfaces list all the required methods, but does not provide....
public and abstract
all of an interface's methods are....
headings for methods
an interface only contains....
the code for all the methods for that interface
when a class implements an interface, the class supplies...
1, but it can implement several interfaces
how many other classes can a class extend?
use the keyword "implements"
how do you implement the interface?
public interface Personalinfo
gets personal info which gets every thjng (getName, getSSN, and getaddress)
method makes a new object with the same state as an existing object
Number of levels of controlling access to fields, methods, and classes.
class provides a field for single-line text entry
a subclass of Object
a class that is defined without an explicit extends clause
Cat, Cat
Cat myCat = new Cat("Snowball");
Animal, Cat
Animal myAnimal = new Cat("Grumpy");
reference type
The __ determines what methods can be called.
the same as the reference type or a child of it
The object type must be __ (HINT: Think about its relation to the reference type.).
Yes, no, "meow", "meow"
Will myCat.scratch() compile? Will myAnimal.scratch() compile? What will myCat.makeSound() print? What will myAnimal.makeSound() print?
Animal, Cat, Cat, will not compile
(d) Cat myCat = new Animal("an animal");
Use an interface.
How do you implement multiple inheritance in Java?
implement all of its methods
If a class implements an interface, it must __.
reference, reference, the ones in that interface
Interfaces can be __ types. If the interface is a __ type, the only methods that object can call are __.
classes, create objects of them
Interfaces aren't __, so you can't __.