SlideShare a Scribd company logo
 Inheritance can be defined as the
process where one class acquires the
properties (methods and fields) of
another .
 The class which inherits the properties
of other is known as Subclass or Child
class or Derived class and the class
whose properties are inherited is
known as Super class or Parent class or
Base class .
 Single Inheritance,
 Multi Level Inheritance,
 Hierarchical Inheritance,
 Multiple Inheritance ,
 Hybrid Inheritance .
(java does not support multiple and
hybrid inheritance because a class cannot
extend more then main class in java).
When a class inherits another class , it
is known as a Single Inheritance.
Class A
Class B
import java.io.*;
import java.util.Scanner;
class cal_bill
{
double billpay;
void Bill(long units)
{
if(units<100)
billpay=units*1.20;
else if(units<=300)
billpay=(100*1.20)+((units-100)*2);
else
billpay=(100*1.20)+(200*2)+((units-300)*3);
}
}
class eb_bill extends cal_bill
{
public static void main(String args[])
{
long units;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of
units:");
units=sc.nextLong();
eb_bill b=new eb_bill();
b.Bill(units);
System.out.println("bill to pay:"+b.billpay);
}
}
enter the number of units:
420
bill to pay:880.0
enter the number of units:
175
bill to pay:270.0
enter the number of units:
89
bill to pay:106.8
When there is a chain of
inheritance , It is known as Multilevel
Inheritance .
Class A
Class B
Class C
import java.io.*;
import java.util.Scanner;
class A
{
int roll,m1,m2,m3;
Scanner s=new Scanner(System.in);
public void display()
{
System.out.println("enter roll number:");
roll=s.nextInt();
System.out.println("enter the mark1:");
m1=s.nextInt();
System.out.println("enter the mark2:");
m2=s.nextInt();
System.out.println("enter the mark3:");
m3=s.nextInt();
}
}
class B extends A
{
int tot,avg;
public void cal()
{
tot=m1+m2+m3;
System.out.println("the total is:"+tot);
avg=tot/3;
System.out.println("the average is:"+avg);
}
}
class C extends B
{
char grade;
public void result()
{
if (avg>90)
grade='O';
else if(avg<90 && avg>=60)
grade='D';
else
grade='U';
System.out.println("the grade is:"+grade);
}
}
public class mark_list {
public static void main(String args[])
{
C c=new C();
c.display();
c.cal();
c.result();
}
}
enter roll number:
5
enter the mark1:
85
enter the mark2:
91
enter the mark3:
79
the total is:255
the average is:85
the grade is:D
When two or more classes inherits a
single class , it is known as Hierarchical
Inheritance .
Class A
Class B Class C
import java.io.*;
import java.util.Scanner;
class X
{
int days;
Scanner l=new Scanner(System.in);
void details()
{
System.out.println("enter the number of days worked:");
days=l.nextInt();
}
}
class Y extends X
{
int d1=500,salary1;
public void supervisor()
{
System.out.println("the supervisor");
System.out.println("the salary for a day is:"+d1);
salary1=days*d1;
System.out.println("the salary for this month is:"+salary1);
}
}
class Z extends X
{
int d2=300,salary2;
public void worker()
{
System.out.println("the worker");
System.out.println("the salsry for a day is:"+d2);
salary2=days*d2;
System.out.println("the salary for this month is:"+salary2);
}
}
public class employee {
public static void main(String args[])
{
int emp_id=0;
Y obj1=new Y();
Z obj2=new Z();
Scanner e=new Scanner(System.in);
System.out.println("enter the employee id:");
emp_id=e.nextInt();
obj1.details();
if (emp_id<100)
obj1.supervisor();
else
obj2.worker();
}
}
 enter the employee id:
543
enter the number of days worked:
28
the worker
the salsry for a day is:300
the salary for this month is:0
 enter the employee id:
75
enter the number of days worked:
26
the supervisor
the salary for a day is:500
the salary for this month is:13000
If subclass (child class) has the same
method as described in parent class , it is
known as Method Overriding in java .
Method overriding is used to provide
specific implementation of a method
which is already provided by its super
class .
Method overriding is used for runtime
polymorphism .
 The method have the same name
as in the parent class .
 The method must have the same
parameter as in the parent class.
 There must be an IS-A
relationship (inheritance) .
import java.io.*;
class vehicle{
void run()
{
System.out.println("Vehicle is running");
}
}
class bike extends vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
bike obj=new bike();
obj.run();
}
}
Bike is running safely
ANY
QUESTIONS?
Inheritance  and overriding

More Related Content

Similar to Inheritance and overriding

Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
Gurpreet singh
 
Inheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdfInheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdf
kshitijsaini9
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
chetanpatilcp783
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
Praveen M Jigajinni
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
Shalabh Chaudhary
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Manish Tiwari
 
6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .
happycocoman
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Abhishek Khune
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
yash jain
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
RithwikRanjan
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
KevinNicolaNatanael
 
Inheritance
InheritanceInheritance
Inheritance
SangeethaSasi1
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
chauhankapil
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
Ahmad sohail Kakar
 

Similar to Inheritance and overriding (20)

Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Inheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdfInheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdf
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .
 
Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)Lecture 14 (inheritance basics)
Lecture 14 (inheritance basics)
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Java unit2
Java unit2Java unit2
Java unit2
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
Inheritance.ppt
Inheritance.pptInheritance.ppt
Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
7_-_Inheritance
7_-_Inheritance7_-_Inheritance
7_-_Inheritance
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 

More from ArchanaMani2

Software evolution and Verification,validation
Software evolution and Verification,validationSoftware evolution and Verification,validation
Software evolution and Verification,validation
ArchanaMani2
 
Code scheduling constraints
Code scheduling constraintsCode scheduling constraints
Code scheduling constraints
ArchanaMani2
 
Ajax enabled rich internet applications with xml and json
Ajax enabled rich internet applications with xml and jsonAjax enabled rich internet applications with xml and json
Ajax enabled rich internet applications with xml and json
ArchanaMani2
 
Excellence in visulization
Excellence in visulizationExcellence in visulization
Excellence in visulization
ArchanaMani2
 
Firewall
FirewallFirewall
Firewall
ArchanaMani2
 
The linux system
The linux systemThe linux system
The linux system
ArchanaMani2
 
Big data
Big dataBig data
Big data
ArchanaMani2
 
Transaction management
Transaction managementTransaction management
Transaction management
ArchanaMani2
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFS
ArchanaMani2
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
ArchanaMani2
 

More from ArchanaMani2 (10)

Software evolution and Verification,validation
Software evolution and Verification,validationSoftware evolution and Verification,validation
Software evolution and Verification,validation
 
Code scheduling constraints
Code scheduling constraintsCode scheduling constraints
Code scheduling constraints
 
Ajax enabled rich internet applications with xml and json
Ajax enabled rich internet applications with xml and jsonAjax enabled rich internet applications with xml and json
Ajax enabled rich internet applications with xml and json
 
Excellence in visulization
Excellence in visulizationExcellence in visulization
Excellence in visulization
 
Firewall
FirewallFirewall
Firewall
 
The linux system
The linux systemThe linux system
The linux system
 
Big data
Big dataBig data
Big data
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Topological Sort and BFS
Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFS
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 

Recently uploaded

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 

Recently uploaded (20)

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 

Inheritance and overriding

  • 1.
  • 2.  Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another .  The class which inherits the properties of other is known as Subclass or Child class or Derived class and the class whose properties are inherited is known as Super class or Parent class or Base class .
  • 3.  Single Inheritance,  Multi Level Inheritance,  Hierarchical Inheritance,  Multiple Inheritance ,  Hybrid Inheritance . (java does not support multiple and hybrid inheritance because a class cannot extend more then main class in java).
  • 4. When a class inherits another class , it is known as a Single Inheritance. Class A Class B
  • 5. import java.io.*; import java.util.Scanner; class cal_bill { double billpay; void Bill(long units) { if(units<100) billpay=units*1.20; else if(units<=300) billpay=(100*1.20)+((units-100)*2); else billpay=(100*1.20)+(200*2)+((units-300)*3); } }
  • 6. class eb_bill extends cal_bill { public static void main(String args[]) { long units; Scanner sc=new Scanner(System.in); System.out.println("enter the number of units:"); units=sc.nextLong(); eb_bill b=new eb_bill(); b.Bill(units); System.out.println("bill to pay:"+b.billpay); } }
  • 7. enter the number of units: 420 bill to pay:880.0 enter the number of units: 175 bill to pay:270.0 enter the number of units: 89 bill to pay:106.8
  • 8. When there is a chain of inheritance , It is known as Multilevel Inheritance . Class A Class B Class C
  • 9. import java.io.*; import java.util.Scanner; class A { int roll,m1,m2,m3; Scanner s=new Scanner(System.in); public void display() { System.out.println("enter roll number:"); roll=s.nextInt(); System.out.println("enter the mark1:"); m1=s.nextInt(); System.out.println("enter the mark2:"); m2=s.nextInt(); System.out.println("enter the mark3:"); m3=s.nextInt(); } }
  • 10. class B extends A { int tot,avg; public void cal() { tot=m1+m2+m3; System.out.println("the total is:"+tot); avg=tot/3; System.out.println("the average is:"+avg); } } class C extends B { char grade; public void result() { if (avg>90) grade='O'; else if(avg<90 && avg>=60) grade='D'; else grade='U'; System.out.println("the grade is:"+grade); } }
  • 11. public class mark_list { public static void main(String args[]) { C c=new C(); c.display(); c.cal(); c.result(); } } enter roll number: 5 enter the mark1: 85 enter the mark2: 91 enter the mark3: 79 the total is:255 the average is:85 the grade is:D
  • 12. When two or more classes inherits a single class , it is known as Hierarchical Inheritance . Class A Class B Class C
  • 13. import java.io.*; import java.util.Scanner; class X { int days; Scanner l=new Scanner(System.in); void details() { System.out.println("enter the number of days worked:"); days=l.nextInt(); } } class Y extends X { int d1=500,salary1; public void supervisor() { System.out.println("the supervisor"); System.out.println("the salary for a day is:"+d1); salary1=days*d1; System.out.println("the salary for this month is:"+salary1); } }
  • 14. class Z extends X { int d2=300,salary2; public void worker() { System.out.println("the worker"); System.out.println("the salsry for a day is:"+d2); salary2=days*d2; System.out.println("the salary for this month is:"+salary2); } } public class employee { public static void main(String args[]) { int emp_id=0; Y obj1=new Y(); Z obj2=new Z(); Scanner e=new Scanner(System.in); System.out.println("enter the employee id:"); emp_id=e.nextInt(); obj1.details(); if (emp_id<100) obj1.supervisor(); else obj2.worker(); } }
  • 15.  enter the employee id: 543 enter the number of days worked: 28 the worker the salsry for a day is:300 the salary for this month is:0  enter the employee id: 75 enter the number of days worked: 26 the supervisor the salary for a day is:500 the salary for this month is:13000
  • 16. If subclass (child class) has the same method as described in parent class , it is known as Method Overriding in java . Method overriding is used to provide specific implementation of a method which is already provided by its super class . Method overriding is used for runtime polymorphism .
  • 17.  The method have the same name as in the parent class .  The method must have the same parameter as in the parent class.  There must be an IS-A relationship (inheritance) .
  • 18. import java.io.*; class vehicle{ void run() { System.out.println("Vehicle is running"); } } class bike extends vehicle { void run() { System.out.println("Bike is running safely"); }
  • 19. public static void main(String args[]) { bike obj=new bike(); obj.run(); } } Bike is running safely