0% found this document useful (0 votes)
3 views27 pages

Understanding Inheritance in OOP

The document discusses the concept of inheritance in object-oriented programming, highlighting how subclasses can inherit and override methods from their superclasses. It explains the use of constructors, polymorphism, method overloading, and the significance of the equals method for comparing objects. Additionally, it covers advanced topics like generics and abstract classes, emphasizing their role in creating flexible and reusable code.

Uploaded by

fgnr4qcy9h
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Understanding Inheritance in OOP

The document discusses the concept of inheritance in object-oriented programming, highlighting how subclasses can inherit and override methods from their superclasses. It explains the use of constructors, polymorphism, method overloading, and the significance of the equals method for comparing objects. Additionally, it covers advanced topics like generics and abstract classes, emphasizing their role in creating flexible and reusable code.

Uploaded by

fgnr4qcy9h
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LEPL1402/LSINC1402

Inheritance
Book
• The following slides are based on chapter 2 "Object-Oriented
Programming" of the book, starting from the section
"Inheritance"

• As before, the slides do not replace reading the book


Last week...
• Last week, we saw
• how to put data that belongs together (employee name
and salary) into an object

• add methods to the class to manipulate the object


(encapsulation)

• On the next slide, you see another example


Private instance variables
(→ encapsulation)

Constructor

Three methods

We call the methods


Using our Weapon class
• With the Weapon class on the previous slide, we can create
as many Weapon instances as we want:
Weapon mySword = new Weapon("Sword", 5);
Weapon dagger = new Weapon("Small knife", 1);
...
int damage = [Link]();

• We can also create an array of weapons:


Weapon[] myWeapons = new Weapon[5];
// Reminder: all array elements are null in a new array
myWeapons[0] = new Weapon("Sword", 5);
myWeapons[1] = new Weapon("Small knife", 1);
...
Creating a new type of weapon
This new class is a subclass
of Weapon

This replaces (overrides) the


getSimpleDamage method of
its superclass Weapon
Super constructor
• Often, it is necessary to call the constructor of the superclass
to make sure that the object is completely initialized

• In our example:

• Here, super(...) = the constructor of the superclass Weapon


Rule 1: A variable of type
Weapon can hold a
reference to an instance of
any subclass of Weapon
Rule 2: A subclass inherits
the methods and instance
variables of its superclass
Rule 3a: A subclass can
override methods of its
superclass
Rule 3b: The JVM will
always call the method
from the "real" class of the
object. The type of the
variable is only used to
verify that the method
exists.
Super method
• You can also use super to call a normal method from the
super class

• A new subclass of Weapon:

Price = price defined in


Weapon class + 100
Adding new instance variables and
methods
• A subclass can also add new instance variables and new
methods that didn’t exist in the super class
Extended constructor

This method only exists in


MagicSword instances, not
in MightySword instances

The compiler will not accept a super class type here


Polymorphism
• We have learned:
• A variable of type Weapon can hold a reference to an
instance of any subclass of Weapon

• Method calls always go to the real class of the object,


independently of the type declared for the variable
("dynamic dispatching")

• These two rules allow us to write objects (or arrays) and


methods that accept instances of different classes as
parameters = Polymorphism
Example

The array can store references


to instances of any subclass of
Weapon

Thanks to dynamic
dispatching, the getPrice()
method of the real class of
the object is called
Method overloading
• It is allowed to have two methods with the same name, but
different parameter types
Method overloading (2)
• If the overloaded methods have parameters from related
classes, the compiler will decide which method to use
based on the type of the variable

No dynamic
dispatching for
parameters!
In the book
• In the book, you will learn how overloaded methods are
called if the argument of the method matches multiple
parameter types
Comparing objects
• Remember: == only compares references
Player player1, player2;
...
if (player1==player2) { // reference to same obj?
...

• For objects, we have to provide our own definition what


equality means in the equals method
Player player1, player2;
...
if ([Link](player2)) {
...
The equals method is inherited
from the Object class.
Object is the superclass of all
classes in Java

It's complicated...

You will learn that later


ArrayList
• The Object class is typically used for the parameters of
methods that can take instances of all classes

• Example: ArrayList is a class from the java standard library


that implements an array to which you can add elements
Boxing
• Primitive types (int, float,...) are not subclasses of Object
• But you can still use them with classes like ArrayList

• Why does this work?


• Putting the 3 in a box: The Java compiler automatically
replaces the int value 3 by an instance of the class Integer
when calling a method that takes an Object or Integer
parameter:
[Link]( new Integer(3) );
Unboxing
• Unboxing doesn’t work automatically with ArrayList. You
cannot write:
ArrayList list = new ArrayList();
[Link](3);
int n = [Link](0); // doesn’t work!

• Why? Because the compiler can only see how the method
get of ArrayList was defined:

• But you can manually do a type cast:


int n = (Integer) [Link](0);
Generics
• Java allows type parameters in class definitions:

This is how ArrayList is really


defined

• Such Generics classes are easier to use:


ArrayList<Integer> list = new ArrayList<Integer>();
[Link](3);
int n = [Link](0);

• The compiler does the typecast for you!


Abstract classes
• Sometimes, the superclass declares a method without an
implementation

The method and the class are


declared as abstract
Abstract classes (2)
• The subclasses provide the implementation of the abstract
methods

Implementation of the abstract


method Another implementation of the
abstract method
Abstract classes (3)
• You cannot create instances of an abstract class
Shape shape = new Shape("My shape");

• But you can use the abstract class type in polymorphic code

The abstract method will be dynamically dispatched to


the implementation in the subclasses

You might also like