Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views15 pages

Lecture 5 - Inheritance

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

Lecture 5 - Inheritance

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

Inheritance

Course Code: CSC1205 Course Title: Object Oriented Programming -


1(JAVA)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 5 Week No: 5 Semester: Summer 21-22


Lecturer: Mazid-Ul-Haque, [email protected]
Lecture Outline

1. Inheritance definition and example


2. Different types of Inheritance
3. Constructor chaining
4. this and super keyword
5. Method Overriding in inheritance
Inheritance
Definition of Inheritance

Super class
Base class Person name,age
Parent class Inheritance

Sub class
Derived class id, roomno
Teacher
Child class

• Inheritance is the process where one class posses the properties of another class.
• Inheritance is used for code reusability.
• To implement parent-child relationship
Inheritance
Definition of Inheritance

A class can be defined as a "subclass" of another class.


 The subclass inherits all data attributes of its superclass
 The subclass inherits all methods of its superclass
 The subclass inherits all associations of its superclass

The subclass can:


• Add new functionality (getY())
• Use inherited functionality (getX())
• Override inherited functionality (getArea())
Inheritance
Definition of Inheritance

When an object is created using new, the system must


allocate enough memory to hold all its instance variables.
This includes any inherited instance variables

In this example, we can say that an Employee "is a kind of"


Person.
An Employee object inherits all of the attributes, methods and
associations of Person
What really happens?
Inheritance
Definition of Inheritance

Person Person
- name: String name = "John Smith"
- dob: Date dob = Jan 13, 1954
Employee
name = "Sally Halls"
is a kind of
dob = Mar 15, 1968
Employee employeeID = 37518
- employeeID: int salary = 65000
- salary: int startDate = Dec 15,
- startDate: Date 2000
Inheritance
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object

public class Person {


Person
- name: String
private String name;
- dob: Date
private Date dob;
[...]

public class Employee extends Person


{ Employee
private int employeID; - employeeID: int
private int salary; - salary: int
private Date startDate; - startDate: Date

[...]

Employee anEmployee = new Employee();


Inheritance
Types of Inheritance
Types of Inheritance

There is no limit to the number of


subclasses a class can have

There is no limit to the depth of the class


tree.
Each Java class has one (and only one)
superclass.
C++ allows for multiple inheritance
Constructors and Initialization
• Classes use constructors to initialize instance variables
• When a subclass object is created, its constructor is called.
• It is the responsibility of the subclass constructor to invoke the
appropriate superclass constructors so that the instance
variables defined in the superclass are properly initialized

• Superclass constructors can be called using the "super" keyword in a


manner similar to "this"
• It must be the first line of code in the constructor

• If a call to super is not made, the system will automatically attempt to


invoke the no-argument constructor of the superclass.
Constructors - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
private float balance;

public BankAccount(int anAccountNumber, String aName)


{
accountNumber = anAccountNumber;
ownersName = aName;
}
[...]
}

public class OverdraftAccount extends BankAccount


{
private float overdraftLimit;

public OverdraftAccount(int anAccountNumber, String aName, float aLimit)


{
super(anAccountNumber, aName);
overdraftLimit = aLimit;
}
}
Method overriding - Example
public class BankAccount
{
private String ownersName;
private int accountNumber;
protected float balance;

public void deposit(float anAmount)


{
if (anAmount>0.0)
balance = balance + anAmount;
}
public void withdraw(float anAmount)
{
if ((anAmount>0.0) && (balance>anAmount))
balance = balance - anAmount;
}

public float getBalance()


{
return balance;
}
}
Method overriding - Example

public class OverdraftAccount extends BankAccount


{
private float limit;

public void withdraw(float anAmount) // Overriding method


{
if ((anAmount>0.0) && (getBalance()+limit>anAmount))
balance = balance - anAmount;
}

}
Books

1. Java Complete Reference, 7th Edition, By Herbert Schildt.

2. A Programmer's Guide to Java SE 8 Oracle Certified Associate, Khalid A. MughalRolf W.


Rasmussen

3. Java How to Program Java, 9th Edition, By Deitel and Deitel.

4. The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and A.


Buckley

5. Introduction to Programming Using Java, 6th Edition, By David j. Eck

6. Head First Java, By Kathy Sierra and Bert Bates


References

1. 1. Java Complete Reference, 7th Edition, By Herbert Schildt.

2. A Programmer's Guide to Java SE 8 Oracle Certified Associate, Khalid A.


MughalRolf W. Rasmussen

2. The Java Tutorials. http://docs.oracle.com/javase/tutorial/

You might also like