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

Inheritance

Uploaded by

bgmiv31
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)
7 views

Inheritance

Uploaded by

bgmiv31
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
You are on page 1/ 7

/*

Inheritance :
~~~~~~~~~~~
Creating a new class from the existing class is known as inheritance.

Existing class -> Parent class / Super class


New Class -> Child class / Derived class

Usages :
1) Code reusability
2) Time saving

Types of inheritance :
~~~~~~~~~~~~~~~~
1) Single inheritance (JAVA)
2) multiple inheritance (x)
3) multilevel inheritance (JAVA)
4) hierarchical inheritance (JAVA)
5) hybrid inheritance. (x)

Single Inheritance :
-----------------------------
Only one parent , only one child.

Parent
|
Child

Multiple Inheritance :
---------------------------------
Multiple parent , only one child

Parent1(int a) parent2 (int a) parent3 (int a)


| | |
-------------------------------------------------------
|
child (a=10)

Multi level Inheritance :


-------------------------------
A class can be derived from another class, but class is a child of another.

Level1
|
Level2
|
Level3
.....
Hiearchical Inheritance :
~~~~~~~~~~~~~~~~~~~~~~~
Single parent, multiple child

Parent
|
---------------------------------------------------------
| | |
child1 child2 child3..

Hybrid Inheritance :
~~~~~~~~~~~~~~~~
Combination of other types.

class1
|
----------------------------------------------------------
| |
class2 class3
| |
----------------------------------------------------------
|
class4

Note :

JAVA will not support multiple parents. So, here we can't use multiple and hybrid inheritances. To
overcome this problem, we can use interfaces .

How to Do inheritance :
~~~~~~~~~~~~~~~~~~~~~~~~
extends -> is used to inherit
Syntax:
class parent
{
....
....
}
class child extends parent
{
......
......
}

child c= new child();


c.functions
Parent Program:
import java.util.*;
class emp
{
int empno; //member variables
String ename,edept;
float sal;

void read() // Member Functions


{
Scanner s = new Scanner(System.in);
System.out.println("Enter the Employee details : ");
empno=s.nextInt();
ename=s.next();
edept=s.next();
sal=s.nextFloat();
}

void display()
{
System.out.println("\nemployee Name : " + ename);
System.out.println("employee no : " + empno);
System.out.println("Department:"+edept);
System.out.println("Salary:"+sal);
}
public static void main(String arg[])throws Exception
{
emp e1,e2,e3;
e1= new emp();
e2= new emp();
e3= new emp();

e1.read();
e2.read();
e3.read();

e1.display();
e2.display();
e3.display();
}

Child program:
class employee extends emp
{
float hra,ta,mda,pf,netsal;
void display2()
{
display();
hra= sal*0.10f;
ta= sal*0.12f;
mda= sal*0.08f;
pf= sal*0.14f;
netsal = sal+hra+mda+ta-pf;
System.out.println("HRA : " + hra);
System.out.println("TA : " + ta);
System.out.println("MDA : " + mda);
System.out.println("PF : " + pf);
System.out.println("NETSAL : " + netsal);
}

public static void main(String[] args) throws Exception


{
employee e1= new employee();
e1.read();
e1.display2();

}
}

Multi level inheritance :


=====================
class vehicle
{
String regno;
String type;
vehicle()
{
regno="TN 09 AX 6666";
type="4 Wheeler - CAR";
}
}

class car extends vehicle


{
String cname, comp, color,make;
int model;
car()
{
cname="Xylo";
comp="Mahendra";
color="Silver";
make="India";
model=2021;
}
}

class Luxurycar extends car


{
String facilities;
Luxurycar()
{
facilities="A/C, DVD, LCD, Power window, theft alarm, accident baloon, etc";
}
void display()
{
System.out.println("Reg.No. : " + regno);
System.out.println("Carname : " + cname);
System.out.println("Company name : " + comp);
System.out.println("Color : " + color);
System.out.println("Year : " + model);
System.out.println("Make: " + make);
System.out.println("Facilities : " + facilities);
}

public static void main(String arg[ ])


{
Luxurycar l=new Luxurycar();
l.display();
}
}

Hierarchical inheritance
========================

import java.util.*;
class person
{
String name,gender;
int age;
Scanner s= new Scanner(System.in);

void read()
{
System.out.println("Enter your name,gender and age : ");
name = s.next();
gender =s.next();
age = s.nextInt();
}

void display()
{
System.out.println("Name : " + name);
System.out.println("Gender : " + gender );
System.out.println("Age : " + age);
}
}

class student extends person


{
String course,duration;
float fee;

void read()
{
System.out.println("Student Details: ");
super.read();
System.out.println("Enter Course name, duration and fee :");
course=s.next();
duration=s.next();
fee =s.nextFloat();
}

void display()
{
super.display();
System.out.println("Course Details :\n\t" + course);
System.out.println("\t" + duration + "\n\t" + fee);
}
void showresult()
{
System.out.println("Student result is pass");
}

class patient extends person


{
String pbm,doc;
void read()
{
System.out.println("Patient Details: ");
super.read();
System.out.println("Enter your problem and consulting doctor name : ");
pbm=s.next();
doc=s.next();
}

void display()
{
System.out.println("Patient Details :" );
super.display();
System.out.println("Problem : " + pbm);
System.out.println("Consulting Doctor: " + doc);
}

class mymain
{
public static void main(String arg[])
{

patient p= new patient();


p.read();
p.display();

student s = new student();


s.read();
s.display();
s.showresult();

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy