Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Inheritance in java

Uploaded by

Dr. Neetu Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Inheritance in java

Uploaded by

Dr. Neetu Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

INHERITANCE IN

JAVA

PRESENTED BY: DR. NEETU SHARMA

PROFESSOR(SCAT)

GUSCSE202232020
INHERITANCE

Inheritance is a fundamental Object-Oriented concept


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: superclass: Person


- name: String
Add new functionality - dob: Date
Use inherited functionality
Override inherited functionality

subclass: Employee
- employeeID: int
- salary: int
- startDate: Date
How is this useful?

Economy of time – When you implement Employee, you already have all
functionality of Person. No need to reimplement
Parameter passing – If you have a function that expects a Person, you
can pass an Employee, and it is still fine.
Fewer special-purpose functions for every type of Person that exists
Container classes (linked lists, binary trees, etc.) can be defined to hold a
Person, and can hold any subclass of Person
Allows limited heterogeneity in container classes
superclass: Person
- name: String
- dob: Date

subclass: Employee
- employeeID: int
- salary: int
- startDate: Date
What really happens?

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

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 in Java

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 Hierarchy

Each Java class has one (and only one) superclass.


C++ allows for multiple inheritance

Inheritance creates a class hierarchy


Classes higher in the hierarchy are more general and more abstract
Classes lower in the hierarchy are more specific and concrete
There is no limit to the Class
number of subclasses a class
can have Class Class Class

There is no limit to the depth


Class Class Class
of the class tree.

Class
The class called Object

At the very top of the inheritance tree is a class called Object


All Java classes inherit from Object.
All objects have a common ancestor
This is different from C++

The Object class is defined in the java.lang package


Examine it in the Java API Specification

Object
PROBLEM DEFINITION
1. Provide three classes named rectangle,
triangle, and circle such that each one of the
classes extends the class shape. Each one of
the classes contains only the method
printarea() that prints the area of the given
shape.

2. Write a java program to create a class called


employee with methods called work() and
getsalary(). Create a subclass called
hrmanager that overrides the work() method

You might also like