Level 114 Level 116
Level 115

[Quiz] Polymorphism


143 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

Ignore?
object-oriented development
The solution to a task
Explain inheritance.
Inheritance means that objects of one class can derive state and/or behavior from another (base or parent) class.
Inheritance is a key concept that underlies ...
IS-A, polymorphism, overriding (not "overloading" but redefine a methodand from a superclass in a subclass) and casting.
Explain polymorphism.
This ability of different but related objects to respond, each in its own way, to identical messages is called polymorphism.
Explain encapsulation.
Encapsulation means that objects keep their state information private. Rather than directly manipulating an object's data, other objects send requests to the object, in the form of messages, some of which the object may res…
inheritance
allows a class to be a subclass of a superclass and thereby inherit public and protected variables and methods of the superclass. A parent/child relationship.
Inheritance provides which of the following?
Allows developers to place general code in a class that more specific classes can gain through inheritance. Also promotes code reuse.
reusability
A feature of object-oriented programming that reduces development time and decreases the likelihood of bugs.
Super Class or Base Class
A class being inherited is referred to by what name(s)?
Base of superclass
What is the name of the class that is being inherited?
super
Similar to the "this" keyword you can use the _______ keyword to refer directly to a superclass
object
instance of a class. A software bundle of related variables and methods used to model real world objects. It has 2 defining characteristics - STATE and BEHAVIOR. State is stored as variables and behavior is represented using methods
Object.
What is the common superclass from which all Java classes derive directly or indirectly?
What are some advantages of all classes deriving from a common class like Object?
Every class inherits all the methods defined in Object. These include methods to get the class name, getClass() and various methods related to synchronization like, wait() or notify() and methods useful for storing an ob…
getClass() returns the class object.
What are some methods defined in the Object class and how are they useful?
What does the *default toString() method* implementation do?
Returns the class name and hash code. If this is not the desired representation of the object, the developer should override it.
Association
Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregatio…
Difference between aggregation and composition
Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the o…
What is a *direct association* relationship?
Direct association describes a "has a" relationship. This is a basic association that represents navigability. Direct association is a weak relationship and therefore can be generalized to an association. The containing object also has…
What is a *composition association*?
Composition association is used to describe relationships where an object is composed of one or more objects. The internal object makes sense only while stored in the internal object.
What is an aggregation association?
An aggregation association is a relationship that represents one object being part of another object. An aggregation association represents a "part of" the whole relationship. In this relationship, even though one is part of…
the *subclass object 'IS-A'* superclass object.
In as 'IS-A' relationship how do the sub and super classes work together?
IS-A relationship is demonstrated
by a class derived from an existing class.
IS-A relationship
the relationship between an object and the class of which it is a member
HAS-A relationship is demonstrated
by a *class that contains another class*.
extends
What keyword at the beginning of a class creates a subclass?
class X is the subcalss
public class *X extends Z* {}
OBJECT TYPE
REFERENCE TYPE and
The visibility of subtype members
In an object variable, what does the reference type determine?
the reference type
In a variable, what corresponds to the type in the variable declaration?
the object type
In a variable, what corresponds to the instantiated class?
True.
In Java, Exceptions are objects. True or false?
What are annotations and how are they useful?
Annotations are pieces of metadata that contain information used by the compiler, associated with an object at run time or compile time. For example, @Deprecated, @Override and @SuppressWarnings.
Abstract classes and interfaces
Which two reference types *cannot be instantiated*?
What is the difference between an abstract class and an interface?
Interfaces don't define methods. Also, classes cannot inherit from more than one class, abstract or not, while a class may implement many interfaces. Both can have constant declarations but in an interface they are implicitly public static and final.
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.
What are some characteristics of an abstract class?
It can not be instantiated. It must be extended. It may or may not contain abstract methods.
must be declared abstract
A class that contains one or more abstract method(s)
abstract method
a method declared in a class or an interface without a method body
abstract
Indicates that the details of a class, a method, or an interface are given elsewhere in the code.
Only one!
How many classes can a class extend?
1
How many classes can a subclass extend directly?
public class A implements B, C {}
If A is a class.....B and C are interfaces, what is the proper signature for class A implements interfaces B and C?
Infinity
How many interfaces can a class implement?
Yes
Does this compile? List<Animal> dogs1 = new ArrayList<Animal>();
As many as it needs.
How many interfaces can a class extend directly?
Where would you use the keyword extends and implements?
extends is the keyword used to declare that this class is a subclass of the class that follows the extends keyword. Implements is a keyword that designates this class as an implementer of the specified interface.
interface (keyword)
The keyword used in a class declaration
interface
What is a group of related methods with empty bodies?
Programming to an interface
What is the concept called in which the developer defines the requirement functionality instead of defining strict object data types?
abstract declarations and constants
Which two members are valid in interfaces?
public
Accessible by any class in the program.
public, static, and final
Which three modifiers are implicit for fields declared in an interface?
implements
What keyword at the beginning of a class groups with an interface?
public class B implements A {
Which declaration for concrete class B indicates it implements the interface A?
all the methods contained in the interface.
A class that implements an interface must implement what else?
//Where Class A uses interface B
Given an interface, how would you make your class behave like the interface.
Interface does not implement ...
the methods *of another interface* but just *extends* them.
public interface A extends B, C {}
With *three interfaces A, B, and C* what is the proper signature for interface A *inherits* interfaces B and C?
extended classes can be used
as a basis for any other class
SubClass extends SuperClass
Which declaration for SubClass indicates its superclass is SuperClass?
Where B is a subclass of A,
How do you derive a new class from a class?
Interface Comparable
A java.lang interface with the
changeable.
What does the greek word polymorphism mean?
General
Does polymorphism make objects more specific or general?
Is polymorphism unidirectional or bidirectional?
Unidirectional. More specific objects can act polymorphically only as more general objects.
Polymorphism
describes the way an object or variable my be treated in different contexts as though it was a different type. For example: inheritance allowes an argument of type B to be passed to a param…
method arguments
What is polymorphism commonly used for?
Polymorphism could be
static and dynamic both.
Polymorphism applies to ...
overriding, not to overloading.
What is meant by method overloading?
Two or more methods have the same name, but specify a different order or data types for parameters.
What is an overloaded method?
Overloaded methods accept different lists of arguments. The argument lists can differ by:
What is method overriding?
A subclass method has the same method signature as the superclass method.
overridden instance methods.
Polymorphic method invocations apply only to ...
Can a public method be overriden by protected method?
No. Overriding methods can NOT be more restrictive than the original method.
When overriding a method that returns a value, what must be true about the data type?
The data type of the overridden method must be compatible with that of the superclass method.
What is method polymorphism?
The actual method that is executed depends on the type of an object, not the reference type of its variable.
Which implementation is executed for overridden methods
In polymorphism, what does the object type determine?
declared abstract and final simultaneously
Abstract methods and classes cannot be ...
What is a constant?
A variable that has a set value at compile time that can never be changed. A java constant is made by adding the final keyword to a class variable, e.g.
Explain how the final keyword is used in Java.
A method declared final may not be overridden. A class declared final may not be extended. A variable declared final may be assigned only once. A static final variable may be used like a const…
Why would you declare a class final?
Making a class final adds some security and consistency to the class behaviour. E.g. by making the String class final, you cannot override its length method. The compiler may optimize the code and mak…
When should a data field be declared final?
Objects with fields declared final can be passed from one thread to another without synchronization. It contributes to robust software design and is slightly faster. Unless there's a good reason why not, all fields sh…
object casting
To cast an object as the appropriate class.
Why must a variable be cast?
To tell the compiler that the data conversion is safe to make
what is the syntax for casting an object?
place the class name in front of its parenthesis
Cast them when moving down. i.e. super to sub class.
Objects need to be cast when they more up or down the inheritance chain? i.e. sub to super etc.
No. Java's autoboxing/unboxing does it automatically.
Is casting needed when going from a primitive type to its wrapper classl?
Runtime exception will occur
What happens if you cast an object to an invalid type?
What is accomplished by reference type casting?
Access to subtype members that are not available through a reference type
The target type must the object type or a valid supertype.
What restriction applies to the target type when performing reference type casting?
What would cause and exception to be thrown from a cast?
If the object was never instantiated as the object it is being cast to or one of its subclasses.
When implementing a method declared in an interface or abstract class, which three techniques are available for specified exceptions?
Not throw an exception, throw the same class of the specified exception, or throw a subclass of the specified exception.
False.
True or False: The enhanced for loop can be used to initialize an array and modify its elements.
non-static nested class (inner class)
In Java, a *nested class* that is *not static* is called an *inner class*. An inner class can interact with the instance of its enclosing class—it has access to private variables and methods of…
difference between inner class and subclass
An inner class is a class that is created inside of another class. A subclass is a class that extends another class.
What is multiplicity?
Multiplicity refers to the number of objects that a part of a relationship. The three general classifications of multiplicity are *one-to-one, one-to-many, and many-to-many*.
What is a temporary association?
A temporary association is also known as a dependency.Typically, a temporary association will be an object used as a local variable, return value, or method parameter. A temporary association is the weakest form of association.
What is association navigation?
Association navigation is a term used to describe the direction in which a relationship can be traveled.
Each source file must have a public class in it.
False. You don't have any public classes in your source files.
What is the difference between the == operator and the equals() method?
== compares two operands and in the case of primitives, returns true if the values are equal. In the case of Objects, it checks to see if the references are the same. The default implementa…
What is a marker interface?
A Marker interface is an empty interface used to indicate that a given class implements a certain functionality. Serializable for example, indicates that a classes non-transient data can be written to an OutputStream. RandomAccess indic…
true
Physical simulators include wind tunnels used to experiment with the design of car bodies.
NO.
Can you define instance and static variables with the same name?
#20 Can a private method of a superclass be declared within a subclass?
Yes. Trick Question!!! Private methodscan NOT be overriden, but they CAN be re-declared/redifined in the subclass, and called directly on the subclass object. But polymorphism will not apply.
new OuterClass.new InnerClass();
True. This may look strange, but that syntax is how do you can create an instance of the InnerClass when you don't yet have an instance of the OuterClass. An InnerClass must ALWAYS be …
#17 Can a top-level class be marked as private?
NO. If a class was private... who could use it? For a normal, top-level class, the only access modifiers that make sense are public and default (package).
#123 Can an abstract method be overriden?
Yes. An abstract method has no choice! It must be overriden.
#110 Each source file must have a public class in it.
NO. You don't have to have any public classes in your source files.
#21 Can an abstract class have non-abstract methods?
Yes. Unlike interfaces, abstract classes CAN provide implementations of methods.
#22 Can a non-abstract class have abstract methods.
NO. Even a single abstract method tells the compiler that the whole class MUST be declared abstract.
#156 True or false? An interface can extend at most one interface.
False. An Interface may extend multiple interfaces. A class is however, limited to extending only one class.
doStuff() in B
doStuff() in A or doStuff() in B?
Y myY = (Y) myX;
Yes. It will compile because the myX object might really have been a Y object all along (as far as the compiler knows), but in runtime it will fail
overriding or overloading
Overriding. An overriding method must have the same name, returntype and arguments as the method in the superclass. It can't declare new or broader checked exceptions, and it also can't have more restrictive access.
#80 Can one object access a private variable of another object of the same class.
Yes. private means "to the class", NOT "private to the object". So two object of the same class could access each other's private data.
#121 Can you have a final abstract class?
NO. final and abstarct classes are opposities: a final class can NOT be subclassed, while an abstract class MUST be subclassed... So final abstract class make not sense
constructor
Used to instantiate (create) an object of that class
instance and static block
Inside abstract class we can take instance and static block.
restriction for abstract class variable
There are no any restriction for abstract class variable.
restriction for interface variable
For the interface variable can not declare variable as private, protected, transient, volatile.
restriction for abstract class method
There are no any restriction for abstract class method modifier that means we can use any modifiers.
restriction for interface method
For the interface method can not declare method as strictfp, protected, static, native, private, final, synchronized.
abstract class is a
collection of abstract method and concrete methods.
interface is a
collection of abstract method
Simplicity and rare-use.
Why doesn't Java support multiple inheritance?
Inheritance is used to place common code in a base class.
what is polymorphism
dynamic, late or runtime binding
no
Does this compile? ArrayList<Animal> dogs1 = new ArrayList<Dog>();
no, only non static methods p203
are object data members polymorphically bound (dynamically)
are static methods polymorphic
no, they are associated with the class, not an object p204
are constructor methods polymorphic
definitely not. 1. they are implicitly static. 2. the hierarchy of constructors is used top to bottom so each class initialises its own objects
are constructor methods instance or static
static, but not explicitly written that way
what is the constructor order in inheritance
static data base to derived, then object data members (order of declaration) and constructor (base to derived)
in reverse order of the initialization
How should a dispose hierarchy be called
the top overriding object's version p210
in the hierarchy of constructor calls, which version of a dynamic method is called
constructors call the overridden version of dynamic methods. what problems result
instance vars of the object are still 0 or null p210
how should you keep base constructors simple
avoid using method calls which could be overridden in derived objects p211
what is a runtime advantage of composition over inheritance p214
you can dynamically swap out objects with composition. inheritance is decided at compile time(different from using polymorphic objects in composition)
what is downcasting
casting an object from a base type to a valid derived type
what kind of polymorphism does the Starship use
composition by swapping elements to alter the behaviour of its interface dynamically
what kind of polymorphism do the Alerts use
inheritance by being different types of a base class
a type derived from the base class method's return type
covariant return types allow overridden methods in a derived class to return