Chapter- 3 Inheritance and Polymorphism-1x4
Chapter- 3 Inheritance and Polymorphism-1x4
Chapter Outlines
Introduction Inheritance
Super-classes and Sub-classes
Using the super Keyword
Method Overriding and Overloading
Polymorphism
Casting Objects and Instance Operator
Inheritance and Polymorphism
The Object Class
Abstract Classes
Interfaces
Using Interfaces
It is compile-time polymorphism.
as long as their signatures are different. public int max(double num1, double num2) {
if (num1 > num2) {
Although we can overload static methods, the arguments or input return num1;
} else {
parameters have to be different. return num2;
We cannot overload two methods if they only differ by a static keyword. }
}
Like other static methods, the main() method can also be overloaded. public int max(int num1, int num2,int num3) {
if (num1 > num2 && num1>num3) {
return num1;
} else if (num2>num3){
Method(X) return num2;
}
Same class else {return num3;}
Method(X,Y)
}
• You can have this three method in
Method(X,Y, Z)
single class
Object Oriented Programming - CoSc2051 25 Object Oriented Programming - CoSc2051 26
Polymorphism Polymorphism
Polymorphism just means that different objects can respond to the
same message in different ways.
Example:
Polymorphism can work for both variables/states and
The human body has different organs. Every organ has a different
methods/behaviors of objects.
function to perform;
However, two powerful polymorphic concepts are often useful when you
class Shapes {
define a class: (Types of Polymorphism-) public void area() {
System.out.println("The formula for area of ");
method overloading and method overriding. }
}
or Compile-time and Run-time class Triangle extends Shapes {
public void area() {
Java implements polymorphism through method overloading and method System.out.println("Triangle is ½ * base * height ");
}
overriding. }
class Circle extends Shapes {
public void area() {
System.out.println("Circle is 3.14 * radius * radius ");
}
Object Oriented Programming - CoSc2051 31 } Object Oriented Programming - CoSc2051 32
5/29/2023
Reference of abstract class is allowed. Abstract class can include Abstract and Non-Abstract methods in them.
They can include constructors and static methods.
subclass must override the abstract method of super class. cannot be extended whereas abstract class is meant for
inheritance.
A class becomes useful if it overrides all the methods of
An Abstract method cannot be final because if it made final then it
abstract class
cannot be overridden whereas Abstract method is meant for
Abstract classes are used for imposing standards and sharing overriding.
methods Abstract Class and method can neither be final nor static.
A Sub class must override an abstract method or else it will become
abstract class.
Interfaces Interfaces
Inheritance is used for borrowing methods. In java a class can extend from one class only but if a class is implementing
Abstract is used for achieving polymorphism as well as Inheritance. an interface then it can implement from multiple interfaces.
Inheritance is completely used for achieving Polymorphism. An interface in Java is a collection of abstract methods and static constants.
Interface can be call as Abstract Class with all abstract methods. As you might know in an interface, each method is public and abstract but it
All the methods are by default abstract. does not contain any constructor.
Classes are extended but Interfaces are implemented. Along with abstraction, the interface also helps to achieve multiple
In Interface we can have reference of interface and the object of the class inheritance in Java.
which is implemented. Note:You can achieve 100% abstraction using interfaces.
Interfaces Interfaces
Interface
Example Program Do’s and Don’ts of Interfaces
interface test1 { By default, methods are Public and Abstract.
void meth1(); Collection of abstract As methods are to be implemented by the classes, they can’t be made
method
void meth2(); private.
}
Identifiers can be used in interfaces but the identifiers must be given in
class test2 implements test1 {
Implementation of
Upper cases.
public void meth1() {}
abstract method in Identifiers are by default final and static.
public void meth2() {} derived class
}
Method inside an interface cannot have body but the method can have
An interface can be extended from another interface. Can have Abstract and Non-Abstract
. Can have only Abstract Methods
Methods
Interface VS Multiple Inheritance
It has only Final Variables It includes Non-Final Variables
In C++ one class can inherit from multiple classes.
It has Static, Non-Static, final, Non-
Multiple Inheritance in java is achieved using Interfaces. It has Static and Final variables only
Final variables
Interfaces are perfect than using Multiple Inheritance. Will not implement the Abstract
Can implement an Interface
Class
Way of thinking in java is more perfect than C++.
Implemented using “implements” Implemented using “extends”
Keyword Keyword
Can extend Java Classes and
Can extend only an Interface
Interfaces
Members can be Private and
Members are Public by default
Protected