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

L14 - Types of Inheritance

OOP

Uploaded by

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

L14 - Types of Inheritance

OOP

Uploaded by

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

CS3391 – OBJECT ORIENTED

PROGRAMMING

UNIT II
LECTURE 14
TYPES OF INHERITANCE

By
Mrs. S. Rajathi
AP / CSE
AAACET-DEPT. OF CSE 1 CS3391 - OOP
SYLLABUS
UNIT II
INHERITANCE, PACKAGES AND INTERFACES
Overloading Methods – Objects as Parameters – Returning
Objects – Static, Nested and Inner Classes. Inheritance:
Basics – Types of Inheritance - Super keyword - Method
Overriding – Dynamic Method Dispatch –Abstract Classes
– final with Inheritance. Packages and Interfaces:
Packages – Packages and Member Access –Importing
Packages – Interfaces.
Content Beyond Syllabus : Legacy Classes and Interfaces

AAACET-DEPT. OF CSE 2 CS3391 - OOP


MULTILEVEL INHERITANCE
• Multilevel inheritance is a process of creating a new sub
class from already inherited sub class.

Super Class

Sub Class

Sub1 Class

AAACET-DEPT. OF CSE 3 CS3391 - OOP


MULTILEVEL INHERITANCE
Syntax : Super Class :
class super_class_name {
data member / field declarations;
method definitions;
}

Syntax : Sub Class :


class sub_class_name extends super_class_name {
data member / field declarations;
method definitions;
}
Syntax : Sub1 Class :
class sub1_class_name extends sub_class_name {
data member / field declarations;
method definitions;
}

AAACET-DEPT. OF CSE 4 CS3391 - OOP


MULTILEVEL INHERITANCE
Example : Super Class :
class A {
int x,y
A
void get() { }
}
Example : Sub Class :
class B extends A {
B
double m,n;
void disp();
}
Example : Sub1 Class :
class C extends B { C
float a,b;
void disp();
}

AAACET-DEPT. OF CSE 5 CS3391 - OOP


MULTILEVEL INHERITANCE –
EXAMPLE PROGRAM
• Write a Java program for the following hierarchy.

Person – name, age, address

Employee – empid, experience , bp,


dra, hra, salary

Faculty – subjects_handled,
ppublished, fdp_attended

AAACET-DEPT. OF CSE 6 CS3391 - OOP


MULTILEVEL INHERITANCE - PROGRAM
import java.util.Scanner;
class Person {
protected String name, address;
protected long age;
Scanner get = new Scanner(System.in);
public void getperson() {
System.out.println("Enter Person name, age & address");
name = get.next();
age = get.nextLong();
address = get.next();
}
public void displayperson() {
System.out.println("Name = "+name);
System.out.println("Age = "+age);
System.out.println("Address = "+address);
}
}
AAACET-DEPT. OF CSE 7 CS3391 - OOP
MULTILEVEL INHERITANCE - PROGRAM
class Employee extends Person {
protected String empid;
protected double exp,bp,da,hra,salary;
Scanner get = new Scanner(System.in);
public void getemp() {
getperson();
System.out.println("Enter Employee id, Experience,
Basic Pay, DA & HRA");
empid = get.next();
exp = get.nextDouble();
bp = get.nextDouble();
da = get.nextDouble();
hra = get.nextDouble();
}
public void calsalary() {
salary = bp + da + hra;
}
AAACET-DEPT. OF CSE 8 CS3391 - OOP
MULTILEVEL INHERITANCE - PROGRAM

public void displayemp()


{
displayperson();
System.out.println("Employee ID = "+empid);
System.out.println("Experience = "+exp);
System.out.println("Basic Pay="+bp+"DA="+da+"HRA="+hra);
calsalary();
System.out.println("Salary = "+salary);
}
}

AAACET-DEPT. OF CSE 9 CS3391 - OOP


MULTILEVEL INHERITANCE - PROGRAM
class Faculty extends Employee {
protected int subhan,ppub,fdpatt;
Scanner get = new Scanner(System.in);
public void getfaculty() {
getemp();
System.out.println("Enter No. of subjects handled,
Papers Published & FDP attended by Faculty");
subhan = get.nextInt();
ppub = get.nextInt();
fdpatt = get.nextInt();
}
public void displayfaculty() {
displayemp();
System.out.println("No. of subjects handled = "+subhan);
System.out.println("No. of Papers published = "+ppub);
System.out.println("No. of FDP attended = "+fdpatt);
}
}
AAACET-DEPT. OF CSE 10 CS3391 - OOP
MULTILEVEL INHERITANCE - PROGRAM

class multilevel {
public static void main(String [] args) {
Faculty F = new Faculty();
F.getfaculty();
F.displayfaculty();
}
}

AAACET-DEPT. OF CSE 11 CS3391 - OOP


MULTILEVEL INHERITANCE
C:\Users\user>javac multilevel.java
C:\Users\user>java multilevel
Enter Person name, age & address
Raja
35
12newstreet

Enter Employee id, Experience, Basic Pay, DA & HRA


EMP50
10
25000
2500
2000

AAACET-DEPT. OF CSE 12 CS3391 - OOP


MULTILEVEL INHERITANCE

Enter No. of subjects handled, Papers Published & FDP


attended by Faculty
5
6
3

AAACET-DEPT. OF CSE 13 CS3391 - OOP


MULTILEVEL INHERITANCE
Name = Raja
Age = 35
Address = 12newstreet
Employee ID = EMP50
Experience = 10.0
Basic Pay=25000.0DA=2500.0HRA=2000.0
Salary = 29500.0
No. of subjects handled = 5
No. of Papers published = 6
No. of FDP attended = 3

C:\Users\user>

AAACET-DEPT. OF CSE 14 CS3391 - OOP


HIERARCHICAL INHERITANCE
• Hierarchical inheritance is a process of creating more
than one sub classes from one super class.

Super Class

Sub1 Class Sub2 Class

AAACET-DEPT. OF CSE 15 CS3391 - OOP


HIERARCHICAL INHERITANCE
Syntax : Super Class :
class super_class_name {
data member / field declarations;
method definitions;
}

Syntax : Sub1 Class :


class sub1_class_name extends super_class_name {
data member / field declarations;
method definitions;
}
Syntax : Sub2 Class :
class sub2_class_name extends super_class_name {
data member / field declarations;
method definitions;
}

AAACET-DEPT. OF CSE 16 CS3391 - OOP


HIERARCHICAL INHERITANCE
Example : Super Class :
class A {
int x,y
public void get() { } A
}
Example : Sub1 Class :
class B extends A {
double m,n;
public void disp();
} B C
Example : Sub2 Class :
class C extends A {
float a,b;
public void disp();
}

AAACET-DEPT. OF CSE 17 CS3391 - OOP


HIERARCHICAL INHERITANCE –
EXAMPLE PROGRAM
• Develop a Java application with Employee class with
Emp_name, Emp_id, address, mail_id, mobile_no as
members. Inherit the classes Programmer,
Asstprofessor, Assoprofessor and Professor from
Employee class. Add Basic pay (BP), DA, HRA, PF &
staffclubamount as members in all inherited classes.
Calculate gross salary with 97% of BP as DA, 10% of BP
as HRA, 12% of BP as PF and 0.1 % of BP for staff club.
Generate pay slip for the employees to display gross
salary and net salary.

AAACET-DEPT. OF CSE 18 CS3391 - OOP


HIERARCHICAL INHERITANCE –
EXAMPLE PROGRAM

Employee - Emp_name, Emp_id, address,


mail_id, mobile_no

Programmer Asstprofessor Assoprofessor Professor

AAACET-DEPT. OF CSE 19 CS3391 - OOP


QUESTION BANK

PART – A
How Many classes can be extended by a class? Write UN, CO2
1
syntax for multilevel inheritance.
2 Define hierarchical inheritance. Write its syntax. RE, CO2
PART – B
Explain multi-level inheritance with an example
UN, CO2
program.

Discuss about Hierarchical inheritance with an UN, CO2


2
example program.

AAACET-DEPT.
September 17, 2022
OF CSE 20 CS3391 - OOP
20
THANK YOU

AAACET-DEPT. OF CSE 21 CS3391 - OOP

You might also like