Inheritance Test (100)
Inheritance Relationship
In an inheritance relationship, the subclass inherits members from the superclass, not the other way around. This means it is not possible for a superclass to call a subclass's method.
Superclass Constructor 2
In an inheritance relationship, the superclass constructor always executes before the subclass constructor. If a superclass has either (a) a default constructor or (b) a no-arg constructor that was written into the class, then that constructor will be automatically called just before a subclass constructor executes.
superclass
In an inheritance relationship, this is the general class.
subclass
In an inheritance relationship, this is the specialized class.
Object Class
The Java API has a class named Object, which all other classes directly or indirectly inherit from. Two of the most useful methods are the toString and equals methods which comes from the Object Class.
super
The following is an explicit call to the superclass's default constructor
Hierarchy
The more general classes are toward the top of the family tree and the more specialized classes are toward the bottom.
is a relationship
a relationship that exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. Ex: allows you to extend the capabilities of a class by creating another class that is a specialized version of it.
A subclass does not have access to these superclass members.
private
Abstract Points
• Abstract methods and abstract classes are defined with the abstract key word. • Abstract methods have no body, and their header must end with a semicolon. • An abstract method must be overridden in a subclass. • When a class contains an abstract method, it cannot be instantiated. It must serve as a superclass. • An abstract class cannot be instantiated. It must serve as a superclass.
overriding
A method in a subclass that has the same signature as a method in the superclass is an example of
Overriding
A subclass may have a method with the same signature as a superclass method. In such a case, the subclass method overrides the superclass method. Also In order for overriding to occur, the subclass method must have the same signature as the superclass method
Inheritance Chain
A superclass can also inherit from another class.
Polymorphism
A superclass reference variable can reference objects of a subclass. In Java, a reference variable is this because it can reference objects of types different from its own, as long as those types are subclasses of its type.
Abstract Class UML
Abstract classes are drawn like regular classes in UML, except the name of the class and the names of abstract methods are shown in italics.
Object
All classes directly or indirectly inherit from this class.
False
All methods in an abstract class must also be declared abstract. T/F
Inheritance
Allows a new class to extend an existing class. The new class inherits the members of the class it extends.
Protected
Protected members of a class may be accessed by methods in a subclass, and by methods in the same package as the class. Protected class members may be denoted in a UML diagram with the # symbol.
Class Header
Public Class (SubClass) Extends (SuperClass)
Subclass
Specialized class. Extended version of Superclass. Inherits fields and methods from Superclass without any of them having to be rewritten. New Fields and methods may also be added to this class
Guidelines
The super statement that calls the superclass constructor may be written only in the subclass's constructor. You cannot call the superclass constructor from any other method. • The super statement that calls the superclass constructor must be the first statement in the subclass's constructor. This is because the superclass's constructor must execute before the code in the subclass's constructor executes. • If a subclass constructor does not explicitly call a superclass constructor, Java will automatically call the superclass's default constructor, or no-arg constructor.
Default Methods
A default method is an interface method that has a body.
Functional Interface
A functional interface is an interface that has one abstract method. You can use a special type of expression, known as a lambda expression, to create an object that implements a functional interface.
overloading
A method in a subclass having the same name as a method in the superclass but a different signature is an example of
overridden
Abstract methods must be
False
In an inheritance relationship, the subclass constructor always executes before the superclass constructor. T/F
True
Every class has a toString method and an equals method inherited from the Object class. T/F
True
Every class is either directly or indirectly derived from the Object class. T/F
final and static
Fields in an interface are
Superclass
General Class of the Program
False
If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method. T/F
False
If two methods in the same class have the same name but different signatures, the second overrides the first. T/F
Package Access
If you do not provide an access specifier for a class member, the class member is given package access by default.
Superclass Constructor 1
These are not inherited as their purpose is to construct objects of the superclass.
Difference
Although overloaded methods have the same name, they have different signatures. When a method overrides another method, however, they both have the same signature. Also: When a subclass overloads a superclass method, both methods may be called with a subclass object. However, when a subclass overrides a superclass method, only the subclass's version of the method can be called with a subclass object.
Abstract Class
An abstract class is not instantiated, but other classes extend it. An abstract method has no body and must be overridden in a subclass. Ex: AccessSpecifier abstract ReturnType MethodName(ParameterList);
True
An abstract class is not instantiated, but serves as a superclass for other classes. T/F
Inner class
An inner class is a class that is defined inside another class. An anonymous inner class is an inner class that has no name. An anonymous inner class must implement an interface, or extend another class.
Interfaces
An interface specifies behavior for a class. An interface cannot be instantiated. Instead, it is implemented by other classes. When a class implements an interface, the class must override the methods that are specified by the interface. All interfaced methods are public.
Interface in UML
In a UML diagram, an interface is drawn like a class, except the interface name and the method names are italicized, and the <<interface>> tag is shown above the interface name. The relationship between a class and an interface is known as a realization relationship (the class realizes the interfaces). You show a realization relationship in a UML diagram by connecting a class and an interface with a dashed line that has an open arrowhead at one end. The arrowhead points to the interface.
first
In a subclass constructor, a call to the superclass constructor must appear
False
Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class. T/F
True
It is not possible for a superclass to call a subclass's method. T/F
Dynamic Binding
Java performs dynamic binding or late binding when a variable contains a polymorphic reference. This means that the Java Virtual Machine determines at runtime which method to call, depending on the type of object that the variable references. So, it is the object's type that determines which method is called, not the variable's type.
Private
Private members of the superclass cannot be accessed by the subclass, so technically speaking, they are not inherited. Only methods in the superclass can access them.
True
Private members of the superclass cannot be accessed by the subclass. T/F
Constructor Issues
The superclass constructor always executes before the subclass constructor. • You can write a super statement that calls a superclass constructor, but only in the subclass's constructor. You cannot call the superclass constructor from any other method. • If a super statement that calls a superclass constructor appears in a subclass constructor, it must be the first statement. • If a subclass constructor does not explicitly call a superclass constructor, Java will automatically call super() just before the code in the subclass's constructor executes. • If a superclass does not have a default constructor and does not have a no-arg constructor, then a class that inherits from it must call one of the constructors that the superclass does have.
instanceof
There is an operator in Java named instanceof that you can use to determine whether an object is an instance of a particular class. EX: refVar instanceof ClassName
protected
These superclass members are accessible to subclasses and classes in the same package.
extends
This key word indicates that a class inherits from another class.
super
This key word refers to an object's superclass.
Super
This key word refers to an object's superclass. You can use the super key word to call a superclass constructor.
instanceof
This operator can be used to determine whether a reference variable references an object of a particular class
False
True or False: A class may only implement one interface.
False
True or False: A subclass reference variable can reference an object of the superclass.
False
True or False: A superclass has a member with package access. A class that is outside the superclass's package but inherits from the superclass can access the member.
True
True or False: A superclass reference variable can reference an object of a subclass that extends the superclass.
False
True or False: An object of a superclass can access members declared in a subclass.
True
True or False: By default all members of an interface are public.
True
True or False: Constructors are not inherited.
False
True or False: If a subclass constructor does not explicitly call a superclass constructor, Java will not call any of the superclass's constructors.
True
True or False: In a subclass, a call to the superclass constructor can only be written in the subclass constructor.
True
True or False: The superclass constructor always executes before the subclass constructor.
True
True or False: When a class contains an abstract method, the class cannot be instantiated.
False
True or False: When a method is declared with the final modifier, it must be overridden in a subclass.
Contract
When a class implements an interface, it is agreeing to provide all of the methods that are specified by the interface. It is often said that an interface is like a "contract," and when a class implements an interface it must adhere to the contract.
Prevention
When a method is declared with the final modifier, it cannot be overridden in a subclass. If a subclass attempts to override a final method, the compiler generates an error. This technique can be used to make sure that a particular superclass method is used by subclasses and not a modified version of it.
True
When a subclass extends a superclass, the public members of the superclass become public members of the subclass. T/F
False
When an interface variable references an object, you can use the interface variable to call any and all of the methods in the class implementing the interface. T/F
dynamic
With this type of binding, the Java Virtual Machine determines at runtime which method to call, depending on the type of the object that a variable references.
Inheritance in UML
You show inheritance in a UML diagram by connecting two classes with a line that has an open arrowhead at one end. The arrowhead points to the superclass.