SlideShare a Scribd company logo
1 of 7
Inheritance:
It is the process ofcreating IS-A relation between classes. in inheritance functionality of a
class are extended by defining a new class which inherits the feature of the existing class
and add some new features of its own.
 Inheritance is used for codereusability and run time polymorphism.
Note:
Constructorof super class cannot be inherited in subclass.
A subclass can either add behavior or behavior and attributes.
Inheritance is unidirectional i.e subclass only inherit the super class.
Types of inheritance:
The following types are supported in java:
The following types are not supported in java
Hybrid and multiple inheritance is not supported for the following
reasons
1. To remove the ambiguity.
2. To provide more maintainable and clear design.
Examples:
in this example only instance variable are inherited in the
derived class A12 from base class A11.
Class A11
{
int a,b;
}
Class A12 extends A11 // A12 inherit A11
{
int c;
Public void show(int x,int y)
{
a=x;
b=y;
}
public void sum()
{
c=a+b;
SOP(“after addition c=”+c);
}
}
Class Test
{
Public static void main(String arr[])
{
A12 a=new A12();
a.show(10,20);
a.sum();
}}
in this example both instance variable and method are inherited
in the derived class A12 from base class A11.
class A11
{
int a,b;
int c;
public void show( int x,int y)
{
a=x;
b=y;
}
}
class A12 extends A11
{
public void sum()
{
c=a+b;
System.out.println(c);
}
}
class A13
{
public static void main(String arr[])
{
A12 a=new A12();
a.show(10,20);
a.sum();
}
}
Define a class vehicle with private instance variable
passenger,fuelcapcity and fuel consumption per km. calculate
the total distance to be covered with in a method by multiplying
fuelcapcity and fuel consumption per km.
define a new subclass truck with instance variable total load
capacity in tons. Display all information using a class
Truckdemo by declaring the method for class truck.
class vechile
{
private int noofpassenger;
private int fuelcap;
private int fuelconskm;
void setP(int p)
{
noofpassenger=p;
}
void setF(int f)
{
fuelcap=f;
}
void setFc(int fc)
{
fuelconskm=fc;
}
int getP()
{
return noofpassenger;
}
int getF()
{
return fuelcap;
}
int getFc()
{
return fuelconskm;
}
int td()
{
return fuelcap*fuelconskm;
}
}
class Truck extends vechile
{
int totalload;
public void display()
{
System.out.println("total load in truck="+totalload);
}
}
class TruckDemo
{
public static void main(String arr[])
{
Truck t1=new Truck();
t1.setP(100);
t1.setF(1000);
t1.setFc(30);
t1.totalload=400000;
System.out.println("total number of passenger="+t1.getP());
System.out.println("total fuel capacity="+t1.getF());
System.out.println("total fuel consumption per km="+t1.getFc());
System.out.println("total distance to be covered="+t1.td());
t1.display();
}
}

More Related Content

Similar to Inheritance

Similar to Inheritance (20)

Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4Effective java-3rd-edition-ch4
Effective java-3rd-edition-ch4
 
Classes & Interfaces
Classes & InterfacesClasses & Interfaces
Classes & Interfaces
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
10. inheritance
10. inheritance10. inheritance
10. inheritance
 
Unit 4 notes.pdf
Unit 4 notes.pdfUnit 4 notes.pdf
Unit 4 notes.pdf
 
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
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Inheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan PasrichaInheritance in c++ by Manan Pasricha
Inheritance in c++ by Manan Pasricha
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
IRJET- Inheritance in Java
IRJET- Inheritance in JavaIRJET- Inheritance in Java
IRJET- Inheritance in Java
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class Inheritance
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Interface
InterfaceInterface
Interface
 
Inheritance and its types explained.ppt
Inheritance and its types  explained.pptInheritance and its types  explained.ppt
Inheritance and its types explained.ppt
 

More from vishal choudhary

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

Inheritance

  • 1. Inheritance: It is the process ofcreating IS-A relation between classes. in inheritance functionality of a class are extended by defining a new class which inherits the feature of the existing class and add some new features of its own.  Inheritance is used for codereusability and run time polymorphism. Note: Constructorof super class cannot be inherited in subclass. A subclass can either add behavior or behavior and attributes. Inheritance is unidirectional i.e subclass only inherit the super class. Types of inheritance: The following types are supported in java:
  • 2. The following types are not supported in java Hybrid and multiple inheritance is not supported for the following reasons 1. To remove the ambiguity. 2. To provide more maintainable and clear design. Examples: in this example only instance variable are inherited in the derived class A12 from base class A11. Class A11 { int a,b; } Class A12 extends A11 // A12 inherit A11 { int c; Public void show(int x,int y) { a=x; b=y;
  • 3. } public void sum() { c=a+b; SOP(“after addition c=”+c); } } Class Test { Public static void main(String arr[]) { A12 a=new A12(); a.show(10,20); a.sum(); }} in this example both instance variable and method are inherited in the derived class A12 from base class A11. class A11 { int a,b; int c; public void show( int x,int y) { a=x; b=y;
  • 4. } } class A12 extends A11 { public void sum() { c=a+b; System.out.println(c); } } class A13 { public static void main(String arr[]) { A12 a=new A12(); a.show(10,20); a.sum(); } }
  • 5. Define a class vehicle with private instance variable passenger,fuelcapcity and fuel consumption per km. calculate the total distance to be covered with in a method by multiplying fuelcapcity and fuel consumption per km. define a new subclass truck with instance variable total load capacity in tons. Display all information using a class Truckdemo by declaring the method for class truck. class vechile { private int noofpassenger; private int fuelcap; private int fuelconskm; void setP(int p) { noofpassenger=p; } void setF(int f) { fuelcap=f; } void setFc(int fc) { fuelconskm=fc; } int getP() {
  • 6. return noofpassenger; } int getF() { return fuelcap; } int getFc() { return fuelconskm; } int td() { return fuelcap*fuelconskm; } } class Truck extends vechile { int totalload; public void display() { System.out.println("total load in truck="+totalload); } } class TruckDemo { public static void main(String arr[]) { Truck t1=new Truck();
  • 7. t1.setP(100); t1.setF(1000); t1.setFc(30); t1.totalload=400000; System.out.println("total number of passenger="+t1.getP()); System.out.println("total fuel capacity="+t1.getF()); System.out.println("total fuel consumption per km="+t1.getFc()); System.out.println("total distance to be covered="+t1.td()); t1.display(); } }