0% found this document useful (0 votes)
136 views

Chapter 3: Inheritance and Polymorphism

1. Inheritance defines a relationship between objects that share characteristics, where a subclass inherits the methods and properties of its superclass. 2. Polymorphism allows a method to be overridden in a subclass, and the correct method will be selected based on the object's class rather than the reference variable's class. 3. Dynamic binding determines which method to call at runtime based on the actual object, whereas static binding makes this determination at compile time.

Uploaded by

Aditya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Chapter 3: Inheritance and Polymorphism

1. Inheritance defines a relationship between objects that share characteristics, where a subclass inherits the methods and properties of its superclass. 2. Polymorphism allows a method to be overridden in a subclass, and the correct method will be selected based on the object's class rather than the reference variable's class. 3. Dynamic binding determines which method to call at runtime based on the actual object, whereas static binding makes this determination at compile time.

Uploaded by

Aditya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 3: Inheritance and Polymorphism

Inheritance:
Inheritance: Defines a relationship between objects that share characteristics.

It is the mechanism by which a new class, called a subclass, is formed from an existing class, called a superclass.

When a subclass inherits characteristics from the superclass, methods and characteristics from the superclass are
available to use by objects created from the subclass.

In addition, a subclass is still usually bigger than a superclass, because (usually) it contains more methods
(constructors, accessors, and mutators), as well as more data.

Inheritance Hierarchy: When the subclass itself is a superclass for another subclass.

Method Overriding: When a subclass redefines a certain method that it has inherited from its superclass. For example,
this may happen if the superclass is Student, and the subclass is GradStudent. Thus, a method in Student that
calculates GPA may be changed and overriden in GradStudent.

Partial Overriding: If part of the original method implementation from the superclass is retained.

Every subclass inherits the public or protected variables and methods of its superclass.

Implementing Subclasses:

public class Student {


// Various Methods
// Constructors
// Public and Protected Variables and Methods
// Private Variables and Methods
}

public class gradStudent extends Student{


// Various Methods are still present
// Constructors are NOT INHERITED
// Public and Protected Variables and Methods are still present
// Private Variables and Methods are NOT INHERITED
}

Extends Keyword:
Extends Keyword: The extends keyword, found right after the declaration of the subclass as in "public class SubClass
extends Superclass"

Inheriting Instance Methods and Variables:


Subclasses do not inherit the private instance variables or private methods of their superclasses. However, objects of
subclasses contain memory for those private instance variables, even though they can't directly access them.
However, a subclass does inherit all the protected and public data members of its parent.

Private data, although previously stated that they can't be directly accessed, are a little bit tricky. They cannot be
directly accessed, but they can be accessed indirectly, ie. the subclass invokes the public accessor or mutator or
constructors that are public in the superclass. Thus, since these methods use the private instance variables, they can
still be accessed in the subclass.

Finally, this is an easy one. Just remember that classes on the same level in a hierarchy diagram do not inherit anything
from each other.

Chapter 3 Inheritance and Polymorphism 1


Method Overriding and the Super Keyword:
Method Overriding: A public method inherited from a superclass can be overriden in a subclass by defining a method
with the same return type and signature (name and parameter types).

Partial Overriding: Partial overriding occurs when the subclass method wants a certain method inherited from the
superclass to do something extra. In this case, the method is once again redefined, as in method overriding, with the
same return type and signature (name and parameter type). Then, inside of its implementation, the keyword super() is
used.

For example, if there is a public method from the superclass called computeGrade(), and you want to add some
functionality to that in the subclass, simply redefine the function (public void computeGrade() {}) and inside of the
braces, write (super.computeGrade()) to import everything from the superclass's version of the computeGrade()
method. Finally, add any other or any additional lines in the subclass's version of computeGrade()'s
implementation.

Constructors and Super


Constructors are never inherited.

The subclass needs its own constructor. Thus, this means that if the subclass constructor is not declared, the program
will attempt to use the superclass default constructor with no parameters. If the superclass has no default constructor
(which is why it's good practice to always put one in), then the compiler will give a compiler error. All assuming, there
is no subclass constructor. Lesson: Create a subclass constructor too!

If this occurs, then additional instance variables in the subclass will be initialized in a default manner. This means
that subclass - specific instance variables that are integers will be 0, strings will be null.

For the equations below, assume in the superclass there are 3 variables: variable1, variable2, and variable3.

// First, let's attempt to create a default subclass


// Next, constructor.
let's attempt to create a parameterized subclass constructor. This one is uniqu
public SubClass() {
super(); public Subclass {
} private int variable4;

public SubClass(int variablea, variableb, variablec, variabled) {


super(variablea, variableb, variablec);
variable4 = variabled;
}
}

If super is used in the implementation of a subclass constructor, it must be used in the first line of the constructor
body.

If no constructor is provided in a subclass, the compiler provdes the following default constructor: This is the same on
the on the left hand side above.

Subclass Rules:
A subclass can add new private instance variables.

A subclass can add new public, private, or static methods.

A subclass can override inherited methods.

A subclass may not redefine a public method as private.

A subclass may not override static methods of the superclass.

A subclass should define its own constructors.

Chapter 3 Inheritance and Polymorphism 2


A subclass cannot directly access the private members of its superclass. It must use accessor or mutator methods.

Polymorphism
A method that has been overriden in at least one subclass is said to be polymorphic. Polymorphism is the mechanism
of selecting the appropriate method for a particular object in a class hierarchy.

The reason polymorphism works is because the correct method is chose due to the class of the object itself, not
the type of the object reference. All that means is that we can have something like this: SuperClass Object1 = new
SubClass();

Remember that the selection of the correct program happens during the run of the program.

Dynamic Binding (Late Binding) vs. Static Binding (Early Binding)


Dynamic binding is a type of binding (essentially the process of the computer deciding which instance method to call)
where a run - type decision, not a compile - time decision, is made.

All this means is that the decision to use a certain method is made while the program is running, not at compile time. It
is important to note that the only elements that go through static binding, when the decision is made during compile -
time, are static, private, and final members (methods and variables).

Overiding vs. Overloading


Overriding occurs when there is a method with the same name as well as the same signature, that is found in a
SuperClass and a SubClass, where it has gone under method or partial overriding. Overriding occurs through dynamic
binding, and here, the object's class, not the variable reference's class, is used when picking out a method.

Overloading occurs when there is a method with the same name but not the same signature found in the same class.
This process is called overloading, and undergoes static binding. Here, the method in the main function corresponds
with the one in the class with the same parameters and signature and name.

Using super in a Subclass


Let us assume there is a SuperClass, and a Subclass, which extends SuperClass. Now, let us say that there is a
common method in both the classes. This method calls another method, also common in both classes. However, this
second method has undergone method overriding, so when this second method is called by the first method, it has to
choose whether to "listen" to the SuperClass's version of that method, or the SubClass's version.

In any case, due to polymorphism, the method will follow the SubClass's version.

Type Compatibility: Downcasting


Downcasting is when a superclass is cast to a subclass type. It is necessary when a subclass's reference variable is
defined in terms of the superclass, and that superclass doesn't have a method that is being called.

For example, Student s = new GradStudent(); is an instance where reference variable "s" represents a GradStudent
object, but is in terms of Student (the superclass).

Thus, s.getID; where getID is a method unique to GradStudent, is unavailable. In order to fix this one may use
downcasting:

Downcasting: GradStudent) s).getID;

You need the outer parentheses because the dot operator holds greater significance than the downcasting, so you
need outer parentheses to make sure the object s gets downcasted from a superclass type to a subclass type.

Chapter 3 Inheritance and Polymorphism 3


The Class Cast Exception
A run - time exception throw to signal an attempt to cast an object to a class of which it is not an instance.

This means you can only downcast to subclass, not any random class.

Abstract Classes
Abstract classes are superclasses that represent an abstract concept.

This means that they should not be instantiated.

An abstract class may contain abstract methods. These are methods that have no implementation code, just a header.
There is no reason or purpose for it in the superclass other than as a placeholder. The method's implementation
appears in the subclasses. If a class contains any abstract methods, it must be declared an abstract class.

The abstract Keyword


Declared with the keyword abstract in the header, and if in case any of the abstract class's subclasses do not method
override the empty method, they too, must be declared abstract classes in order to avoid a compile - time error.

It also must be present in any abstract method's declaration, wherever that declaration of that method with no
implementation is.

It is also possible for an abstract class to have no abstract methods. This occurs when an abstract subclass of an
abstract superclass inherits the abstract methods without explicitly declaring them.

An abstract class may or may not have constructors. In addition, no instances can be created for an abstract class.
However, the reference variable may belong to the abstract class, its just that the object it references must be one of a
class that is not abstract.

Interfaces
An interface is a collection of related methods. They are must have only abstract methods, and may not contain any
instance variables. They may, however, contain static final variables, and static final variables only.

Interfaces, unlike abstract classes do not group together similar objects in terms of what they are representing, and
instead group together similar objects in terms of what they can / supposed to do.

For instance, if there is a game of battleship, there may be a public abstract class called Ship, with public classes
Battleship, Destroyer, etc..

However, the methods that such classes will need include moveuponespace(), movedownonespace() as these ships
need to move up and down on the 2D grid. Thus, for this, you can have an interface using the implements
keyword, which can be used for any object that can move up and down on the 2D plane, including fish, sharks, and
ships, which are not common in nature but common in the way they can move on the 2D plane

Chapter 3 Inheritance and Polymorphism 4

You might also like