0% found this document useful (0 votes)
2 views21 pages

13 Inheritance

The document covers the concept of inheritance in Object-Oriented Programming (OOP) using Java, explaining how subclasses can inherit and enhance the properties of superclasses. It discusses direct and indirect inheritance, single vs. multiple inheritance, and the importance of access modifiers and method overriding. Additionally, it introduces the java.lang.Object class, which is the root of the class hierarchy in Java, detailing its key methods such as equals(), hashCode(), and toString().
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)
2 views21 pages

13 Inheritance

The document covers the concept of inheritance in Object-Oriented Programming (OOP) using Java, explaining how subclasses can inherit and enhance the properties of superclasses. It discusses direct and indirect inheritance, single vs. multiple inheritance, and the importance of access modifiers and method overriding. Additionally, it introduces the java.lang.Object class, which is the root of the class hierarchy in Java, detailing its key methods such as equals(), hashCode(), and toString().
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

Object-Oriented Programming

CSC 2306 02

Instructor: Dr. Oumaima Hourrane


Email: [Link]@[Link]
School of Science and Engineering
Unit 4 Inheritance
with Java

2
Inheritance
• Software reusability (with no copy-paste):
• Create new class (subclass) from existing class (superclass)
• Absorb existing class’s data and behaviors
• Enhance with new capabilities (additional attributes and behaviors)
• Behaviors inherited from superclass can be also customized

• Inheritance creates an is-a relationship, meaning the child is a more specific


version of the parent
• Object of one class “is an” object of another class

OOP - CSC 2306 3


Direct vs. Indirect inheritance

• Class hierarchy
• Direct superclass
• Inherited explicitly (one level up hierarchy)
• Indirect superclass
• Inherited two or more levels up hierarchy

• Java supports both direct and indirect inheritance

OOP - CSC 2306 4


Single vs. multiple inheritance

• Single inheritance
• Subclass inherits from one superclass

• Multiple inheritance
• Subclass inherits from multiple superclasses
• Java does NOT support multiple inheritance
• You can instead inherit from one class and implement many
interfaces (discussed later)

OOP - CSC 2306 5


Inheritance – design level

In UML class diagram,


an open arrowhead
points from the
derived subclass to
the parent superclass

OOP - CSC 2306 6


Inheritance – Java
In Java:
• we use the reserved word extends to establish an inheritance relationship
class CommissionEmployee extends Employee {
// class body
}
• The subclass inherits public and protected members from the superclass.
• Objects of subclasses contain private fields of their superclasses, but the subclass
itself has NO NOTION of private fields of its superclass.

OOP - CSC 2306 7


Inheritance – Java Example
class Employee {
public String name;
protected double salary;
public void work() {
[Link](name + " is working");
}
}
class CommissionEmployee extends Employee {
public double commissionRate;
public void calculatePay() {
double pay = salary * commissionRate;
[Link]("Pay: " + pay);
}
}

OOP - CSC 2306 8


Why Prefer Private Over Protected for Attributes?

• Use the protected access modifier when a superclass should provide a direct access to its
subclasses and other classes in the same package, but not to other clients.

• BUT, Declaring superclass instance variables private (as opposed to protected) enables the
superclass implementation of these instance variables to change without affecting
subclass implementations.

• When possible, DO NOT include protected instance variables in a superclass. Instead,


include non-private methods that access private instance variables. This will ensure that
objects of the class maintain integrity of their data.

OOP - CSC 2306 9


Inheritance – Java

• Constructors are NOT inherited


• To initialize the parent class, you need to call the parent’s constructor explicitly in the
child’s constructor.
• The first line of the child’s constructor should be: super(argumentlist);
• If you don’t call the parent’s constructor, Java automatically tries to call the parent’s no-
argument constructor.
• If the parent doesn’t have a constructor, you’ll get a syntax error.

OOP - CSC 2306 10


class Employee {
String name;
double salary;
public Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
}

Inheritance – class Manager extends Employee {


String department;
Java Example public Manager(String name, double salary, String
department) {
super(name, salary); // calling the Employee constructor
[Link] = department;
}
}
public class Main {
public static void main(String[] args) {
Manager mgr = new Manager("Alice", 90000,
"Sales");
OOP - CSC 2306 11
}
Method Overriding in Java
• Java subclasses may redefine inherited methods, this is called overriding methods
• Overriding lets subclasses behave in a special, more specific way
• Overriding only changes how the method works in the subclass and its children.
• The overridden method must have the same signature as the parent’s method, but can
have a different body
• You can call the parent’s version of the method using [Link]() to add to
its behavior.
• Do NOT confuse overriding with overloading (which uses different parameters).
• @Override annotation informs the compiler that you're overriding a method.

OOP - CSC 2306 12


Access Modifiers in Overriding

• You can’t override a method with a more restrictive access modifier.


• A public method in the parent class must stay public in the subclass—it can’t become
protected or private.
• A protected method in the parent class can’t become private in the subclass.
• This keeps the "is-a" relationship, ensuring that the subclass can still respond to method
calls from the parent class.

OOP - CSC 2306 13


Final methods and classes
• final methods
• Declared with the final keyword
• Cannot be overridden in a subclass
• private and static methods are implicitly final

• final classes
• Declared with the final keyword, for example [Link] is final in the java API:
public final class String {…}
• Cannot be extended by a subclass
• All methods in a final class are implicitly final

OOP - CSC 2306 14


Unit 4 The class
Object

15
The class Object
Class [Link] is the root of the class hierarchy. Every class has Object as a
superclass. All objects, including arrays, implement the methods of this class.
Methods:
• Clone()
• equals()
• finalize()
• getClass()
• hashCode()
• Notify(), notifyAll(), wait()
• toString()

OOP - CSC 2306 16


The class Object – hashCode ()

public String hashCode()

• Provides a numeric representation of an object’s contents


• This method is supported for the benefit of hash tables.
• The hashCode default implementation returns an integer that represents the
internal memory address of the object

OOP - CSC 2306 17


The class Object – getClass ()

public final [Link] getClass()

• returns an instance of the [Link] class, which contains information


about the class of the object that getClass() was called from
• getClass() cannot be overriden because it is declared as a final method
• It is particularly useful to library programmers because it is essential for the
concept known as reflection.

OOP - CSC 2306 18


The class Object – toString ()

public String toString()

• Returns a string representation of the object, containing mainly the state of the
object
• It is recommended that all subclasses override this method

OOP - CSC 2306 19


The class Object – equals()

public boolean equals(Object obj)

• Should return true if the two objects have the same content, false otherwise
• The default implementation compares if two references point to the same object:
[Link] (y) compares x==y
• We can override equals in any class to define equality in some more appropriate
way

OOP - CSC 2306 20


Q&A

For Further questions and inquiries


please reach out! ([Link]@[Link])
Office Place: Bldg 8B, Room 205

21

You might also like