Iheritance & Polymorphism Flashcards
Inheritance
The is-a relationship between a more general superclass and a more specialized subclass. Inheritance allows a new class to extend an existing class. By doing so, the new class inherits the members of the class it extends.
Superclass
The general class from which more specialized classes are derived (AKA a base class)
Subclass
A specialized class that is derived from a superclass (AKA a derived class)
Overriding superclass methods
If a subclass has a method with the same signature as a superclass then the subclass method overrides the superclass method. Because the subclass is more specialized than the superclass, sometimes it’s necessary for the subclass to replace inherited methods with more suitable ones.
What is the difference between overloading and overriding?
Overloading is when a method has the same name as one or more other methods, but different parameter lists. That is, they have different signatures. When a method overrides another method, however, they have the same signatures.
How can you protect a method from being overridden?
Declare it with the final modifier. For example: public final void message()
Protected members
Members of a class that may be accessed by methods in a subclass and by methods in the same package as the class. The access is somewhere in between public and private.
Chains of inheritance
A superclass can also inherit from another class
The Object class
The Java API has an Object class from which all other classes directly or indirectly inherit. As a result, every class inherits its members. Two of the most useful are the toString and equals methods, which are available to every class you create since they’re members of the Object class.
Polymorphism
This term means the ability to take on many forms. In Java, a reference variable is polymorphic because it can reference objects of different types from its own, as long as those types are subclasses or its type. That is, a superclass reference variable can reference objects of a subclass.
Dynamic binding / dispatch
Binding is the process of matching a method call with the correct method definition. Java performs dynamic, or late, binding when a variable contains a polymorphic reference. The JVM determines which method to call at runtime based on the type of object that the variable references. Therefore it is the object’s type that determines which method is called not the variable’s type. This is one of the hallmark features of object oriented programming.
The instanceOf operator
This can be used to determine whether an object is an instance of a particular class. The general form of an expression that uses it is:
refVar instanceOf ClassName
For example: if (activity instanceOf GradedActivity)
Abstract class
A class that is not instantiated, but other classes extend it. That is, it serves as a generic or abstract form of all the classes that inherit from it. For example, an Airplane class may act as a superclass from which subclasses for specific models may be derived. It is a superclass.
A class becomes abstract when you place the abstract key word in the class definition, such as:
AccessSpecifier abstract class ClassName
For example:
public abstract class Student
Abstract method
A method that has no body and must be overridden in a subclass. The general format is:
AccessSpecifier abstract ReturnType MethodName(ParameterList);
They are used to ensure that a subclass implements the method.
Interface
Specifies behavior for a class. It is like a class that contains only abstract methods. An interface cannot be instantiated. Instead, it is implemented by other classes. Therefore the syntax is:
public class MyClass implements Interface
For example:
public class Sneakers implements Shoes
Note that multiple interfaces can be implemented:
public class MyClass implements Interface1, Interface2, …….
An interface is like a contract since when a class implements an interface it is agreeing to provide all of the methods that it specifies. In a UML diagram, an interface is drawn like a class, except the interface name and the method names are italicized and the <> tag is shown above the interface name.
Default method
An interface method that has a body. When a class implements an interface with default method, the class can override the default method but it is not required to. They are declared with the key word “default” such as: default void display() One benefit is that they allow you to add new methods to an existing interface without causing errors in the classes that already implement the interface.
Anonymous class
A class that does not have a name.