Iheritance & Polymorphism Flashcards

1
Q

Inheritance

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Superclass

A

The general class from which more specialized classes are derived (AKA a base class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Subclass

A

A specialized class that is derived from a superclass (AKA a derived class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Overriding superclass methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between overloading and overriding?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you protect a method from being overridden?

A

Declare it with the final modifier. For example: public final void message()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Protected members

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Chains of inheritance

A

A superclass can also inherit from another class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The Object class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Polymorphism

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Dynamic binding / dispatch

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The instanceOf operator

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Abstract class

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Abstract method

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Interface

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Default method

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Anonymous class

A

A class that does not have a name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Anonymous inner classes

A

An inner class is a class that’s defined inside another class. An anonymous inner class is an inner class that has no name. It must implement an interface or extend another class.

19
Q

Anonymous object

A

An object that is not stored in a named variable.

20
Q

Functional interface

A

An interface that has one abstract method. You use a lambda expression to create an object that implements a functional interface. These are new features in Java 8 to simplify code. An example of a functional interface is:

interface IntCalculator

{

int calculate(int number);

}

21
Q

Why are both abstract classes and interfaces needed in Java if they’re so similar to each other?

A

Because a class can only extend one superclass but Java allows a class to implement multiple interfaces.

22
Q

What is the relationship between a class and an interface and how is represented in UML?

A

It is known as a realization relationship (the class realizes the interface). You show this in UML 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.

23
Q

Interface type reference variables

A

Just as you can create reference variables of a class type, Java allows you to create reference variables of an interface type. An interface reference variable can reference any type of object that implements that interface, regardless of its class type. This is another examples of polymorphism.

24
Q

What is a limitation of interface reference variables?

A

You can only use them to call methods that are specified in the interface.

25
Q

Lambda expression

A

They provide an easy way to implement anonymous inner classes. They can be thought of as an anonymous method or a method with no name. Like regular methods, lambda expressions can accept arguments and return values. This is the format is a simple lambda expression that accepts one argument and returns a value:

parameter -> expression

Where the operator above is known as a lambda operator, the parameter represents the argument passed to the functional interface’s single method, and the expression is the value that’s returned by the method. Here is an example: X -> X * X

You can use this to simplify the code in the prior example of a functional interface:

IntCalculator square = X -> X * X;

26
Q

What does “effectively final” mean?

A

It’s a variable that hasn’t been declared with the final key word but whose value is never changed.

27
Q

How can a subclass method call an overridden superclass method?

A

By prefixing its name with the “super” key word. For example: super.setScore(rawScore * percentage);

28
Q

If a method in a subclass has the same signature as a method in the superclass, does the subclass method overload or override the superclass method?

A

Override

29
Q

How do you prevent a method from being overridden?

A

Declare it with the “final” modifier

30
Q

What is the purpose of an interface?

A

To specify behavior for other classes

31
Q

How is an interface similar to an abstract class?

A

Neither can be instantiated

32
Q

How is an interface different from an abstract class, or any class?

A

An interface only specifies methods; it does not define them. Also, all members of an interface are public.

33
Q

If an interface has fields, how are they treated?

A

As final and static

34
Q

Concrete class

A

A class that can be instantiated

35
Q

Comparable interface

A

This is one of the most common inbuilt interfaces in Java.

36
Q

Compare interfaces and inheritance

A

Inheritance is used to model hierarchies of related classes (for example, public class Rose extends Flower), while interfaces model common aspects of classes (implements Comparable).

A few other differences are:

  • A class can only extend (inherit from) a single superclass, but can implement multiple interfaces
  • A superclass provides some implementation that the subclass inherits, but an interface provides no implementation
37
Q

How do you call a superclass’s method from a subclass?

A

With the keyword super. For example, in the class Goalkeeper extends Player, you call the kick( ) method from the Player class with super.kick( );

38
Q

How does constructor inheritance work?

A

Subclasses try calling their superclass’s default constructor (this happens implicitly without passing any parameters). However, to prevent calling the default constructor, you can make an explicit call, which requires one of two key words: this or super.

  • super( ) calls the superclass’s default constructor
  • super(parameters) calls the superclass’s constructor with parameters
  • this key word calls the subclass’s constructor

For example:
public Goalkeeper(String name) {
super(name);
}

Where, super calls the Player class's constructor:
public Player(String playerName) {
 name = playerName;
}
39
Q

From which class do all classes in Java inherit?

A

The Object class.

40
Q

What is the Liskov substitution principle?

A

If ‘S’ is a subtype of ‘T’, then objects of type ‘T’ may be replaced with objects of type ‘S’ without changing the behavior of the program.

That is, a subclass object can be used in place of a superclass object.

41
Q

Compile time polymorphism

A

The type of the object is determined at the compile time by the compiler itself (AKA static binding). Method overloading is an example of compile time polymorphism.

42
Q

Runtime polymorphism

A

The overridden method is resolved at runtime rather than compile-time (AKA dynamic binding). In this, a reference variable is used to call an overridden method of a superclass at run time. An example of runtime polymorphism is method overriding.

43
Q

Why overload a method?

A
1. To provide a single name for multiple methods with similar behavior. For example:
 println(int), println(double), println(String, etc.
2. To supply default values. For example:
 int increment (int amount) {
 count = count + amount;
 return count;
 }
 int increment( ) {
 return increment(1);
 }
 \*Note that one method can call the other.
3. Make your code adhere to the DRY principle better.
44
Q

What is a common use of overloading?

A

To supply multiple constructors for a class when there are a large amount of potential instance variables.