SlideShare une entreprise Scribd logo
1  sur  13
Inheritance and Polymorphism
M. Raihan
Computer Science and Engineering Discipline , Khulna University
What is 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 can:
 Add new functionality
 Use inherited functionality
 Override inherited functionality
Declaration
 Inheritance is declared using the "extends" keyword
 To access Super Class from Sub Class we use super keyword.
Example :
class SuperClass{
// rest of codes
}
class SubClass extends SuperClass{
// rest of codes
}
Inheritance Hierarchy
 Each Java class has one (and only one) superclass.
 Inheritance creates a class hierarchy
Class A
Class B
Class D Class E
Class C
Class F
A Program
public class SuperClass {
Scanner sc = new Scanner(System.in); // Here we take input from Keyboard
public double CircleArea(){
double pi=3.1416,num1=0,r=0,CircleAreaResult=0;
num1=sc.nextDouble(); // num1 input taken
r=sc.nextDouble(); // r input taken
CircleAreaResult = pi*num1*(r*r);
return CircleAreaResult;
}
public double TriangleArea(){
double base,hight,TriangleAreaResult = 0;
base=sc.nextDouble(); // Here base input taken keyboard
hight=sc.nextDouble(); // hight input taken from keyborad
TriangleAreaResult = 0.5*base*hight;
return TriangleAreaResult;
}
public void Result() {
double Result1,Result2;
Result1= CircleArea();
Result2= TriangleArea();
System.out.println(“Circle Area : “+Result1+”nTriangle Area : “+Result2);
}
}
A Program (continue)
public class Main extends SuperClass{
public static void main(String[] args) {
SuperClass sp= new SuperClass();
sp.Result();
}
}
Some Methods
toString(): return a string that contains the name
of the object’s class and a hash value
equals(): determines if two variables point to the
same object (more shortly).
Access Modifiers
 Private data fields are not accessible to derived classes
 Protected visibility allows data fields to be accessed either by the
class defining it or any subclass.
 In general, it is better to use private visibility because
subclasses may be written by different programmers and
it is always good practice to restrict and control access to
the superclass data fields
 Members (variables and methods) declared with public
visibility are accessible, and those with private visibility
are not
Method Overloading
When 2 or more methods in a class have the same method
names with different arguments, it is said to be method
overloading.
Overloading does not block inheritance from the superclass.
Overloaded methods must have different method
signatures.
Example
public class Calculate{
public void circle(){
}
public int circle(int a){
return ;
}
public float circle(float a){
return ;
}
}
Method Overriding
When a method in a class has the same method
name with same arguments as that of the
superclass, it is said to be method overriding.
Overriding blocks inheritance from the superclass.
Overridden methods must have same signature.
Example
public class SuperClass{
public void calculate(int a, int b){
int c=a+b;
}
}
public class SubClass extends SuperClass{
public void calculate(int a, int b){
int c;
if(a>b)
c=a-b;
else
c=a+b;
}
}
Thank You

Contenu connexe

Tendances

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 

Tendances (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 

En vedette

JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA PolymorphismMahi Mca
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overridingJavaTportal
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceOum Saokosal
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism023henil
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract ClassOUM SAOKOSAL
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphismTOPS Technologies
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 

En vedette (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
Inheritance
InheritanceInheritance
Inheritance
 
polymorphism
polymorphism polymorphism
polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 
Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
 
Java API
Java APIJava API
Java API
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 

Similaire à Inheritance and Polymorphism Java

Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance SlidesAhsan Raja
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritanceraksharao
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2sotlsoc
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extraNurhanna Aziz
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance ConceptsVicter Paul
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javachauhankapil
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 

Similaire à Inheritance and Polymorphism Java (20)

Java presentation
Java presentationJava presentation
Java presentation
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Java
JavaJava
Java
 
Chap11
Chap11Chap11
Chap11
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extra
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Core java oop
Core java oopCore java oop
Core java oop
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 

Plus de M. Raihan

Amplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers newAmplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers newM. Raihan
 
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...M. Raihan
 

Plus de M. Raihan (20)

Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Amplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers newAmplifiers and biopotential amplifiers new
Amplifiers and biopotential amplifiers new
 
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...Comparison of fnir with other neuroimaging modalities relation between eeg sy...
Comparison of fnir with other neuroimaging modalities relation between eeg sy...
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 

Dernier

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
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).pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
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 . pdfQucHHunhnh
 
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.christianmathematics
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Dernier (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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
 
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.
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Inheritance and Polymorphism Java

  • 1. Inheritance and Polymorphism M. Raihan Computer Science and Engineering Discipline , Khulna University
  • 2. What is 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 can:  Add new functionality  Use inherited functionality  Override inherited functionality
  • 3. Declaration  Inheritance is declared using the "extends" keyword  To access Super Class from Sub Class we use super keyword. Example : class SuperClass{ // rest of codes } class SubClass extends SuperClass{ // rest of codes }
  • 4. Inheritance Hierarchy  Each Java class has one (and only one) superclass.  Inheritance creates a class hierarchy Class A Class B Class D Class E Class C Class F
  • 5. A Program public class SuperClass { Scanner sc = new Scanner(System.in); // Here we take input from Keyboard public double CircleArea(){ double pi=3.1416,num1=0,r=0,CircleAreaResult=0; num1=sc.nextDouble(); // num1 input taken r=sc.nextDouble(); // r input taken CircleAreaResult = pi*num1*(r*r); return CircleAreaResult; } public double TriangleArea(){ double base,hight,TriangleAreaResult = 0; base=sc.nextDouble(); // Here base input taken keyboard hight=sc.nextDouble(); // hight input taken from keyborad TriangleAreaResult = 0.5*base*hight; return TriangleAreaResult; } public void Result() { double Result1,Result2; Result1= CircleArea(); Result2= TriangleArea(); System.out.println(“Circle Area : “+Result1+”nTriangle Area : “+Result2); } }
  • 6. A Program (continue) public class Main extends SuperClass{ public static void main(String[] args) { SuperClass sp= new SuperClass(); sp.Result(); } }
  • 7. Some Methods toString(): return a string that contains the name of the object’s class and a hash value equals(): determines if two variables point to the same object (more shortly).
  • 8. Access Modifiers  Private data fields are not accessible to derived classes  Protected visibility allows data fields to be accessed either by the class defining it or any subclass.  In general, it is better to use private visibility because subclasses may be written by different programmers and it is always good practice to restrict and control access to the superclass data fields  Members (variables and methods) declared with public visibility are accessible, and those with private visibility are not
  • 9. Method Overloading When 2 or more methods in a class have the same method names with different arguments, it is said to be method overloading. Overloading does not block inheritance from the superclass. Overloaded methods must have different method signatures.
  • 10. Example public class Calculate{ public void circle(){ } public int circle(int a){ return ; } public float circle(float a){ return ; } }
  • 11. Method Overriding When a method in a class has the same method name with same arguments as that of the superclass, it is said to be method overriding. Overriding blocks inheritance from the superclass. Overridden methods must have same signature.
  • 12. Example public class SuperClass{ public void calculate(int a, int b){ int c=a+b; } } public class SubClass extends SuperClass{ public void calculate(int a, int b){ int c; if(a>b) c=a-b; else c=a+b; } }