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

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 isdeclared 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  EachJava 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 classSuperClass { 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) publicclass Main extends SuperClass{ public static void main(String[] args) { SuperClass sp= new SuperClass(); sp.Result(); } }
  • 7.
    Some Methods toString(): returna 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  Privatedata 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 2or 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{ publicvoid circle(){ } public int circle(int a){ return ; } public float circle(float a){ return ; } }
  • 11.
    Method Overriding When amethod 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{ publicvoid 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; } }
  • 13.