Chapter - 3 Inheritance and Polymorphism
Chapter - 3 Inheritance and Polymorphism
OOP- CoSc3112 1
Inheritance and Polymorphism
re-use
Dog Cat
String name String name
int fleas int hairballs
String getName() String getName()
int getFleas() int getHairballs()
void speak() void speak()
using
inheritance
superclass
Animal
String name subclass
subclass String getName()
Dog Cat
int fleas int hairballs
int getFleas() int getHairballs()
void speak() void speak()
Calling
double Superclass Constructors:
width;
double height;
double Adepth;
constructor is used to construct an instance of a class.
Unlike properties and methods, the constructors of a superclass are
// constructor used when no dimensions specified
not inherited by a subclass.
Box() {
width = -1; // use -1 to indicate
They =can
height only
-1; // be
aninvoked from the constructors of the subclasses
uninitialized
depth = -1; // box
} using the keyword super.
// constructor
The syntax used when
to call all dimensions
a superclass’s specified
constructor is:
Box(double w, double h, double d) {
super(),
width = w; heightor = h;super(parameters);
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
} Object Oriented Programming - CoSc2051 13
Introduction to Inheritance
public
// class now
BoxWeight Box uses
{ super to initialize its Box attributes.
public class BoxWeight extends Box{
Calling
double Superclass Constructors:
width;
double weight;
double height; // weight of box
double Adepth;
constructor is used to construct an instance of a class.
// default
Unlike constructor
properties and methods, the constructors of a superclass are
// constructor
BoxWeight() { used when no dimensions specified
Box() {not inherited by a subclass.
super();
width == -1;
weight -1; // use -1 to indicate
} They =can
height -1;only
// be
aninvoked from the constructors of the subclasses
uninitialized
depth = -1; // box
using the keyword super.
} initialize
// width, height, and depth using super()
// constructor
BoxWeight(double
The syntax used
w, when
double
to call all
h, dimensions
double constructor
a superclass’s specified
d, double m)
is: {
Box(double
super(w,w, h,double
d); //h, double
call d) {
superclass constructor
super(),
width
weight m; heightor
== w; = h;super(parameters);
depth = d;
} }
}// compute and return volume
double volume() {
return width * height * depth;
}
} Object Oriented Programming - CoSc2051 14
Introduction to Inheritance
public
public class
class Box {
TestDemoSuper {
Calling
double
public Superclass
width;
static Constructors: args) {
void main(String[]
double height;
Adepth;
double constructor is used to construct an instance of a class.
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
Unlike
BoxWeight properties
mybox2 = newand methods, the//
BoxWeight(); constructors
default of a superclass are
// constructor used when no dimensions specified
Box()
double{not inherited by a subclass.
vol;
vol=width = -1; // use -1 to indicate
mybox1.volume();
They =can
height only
-1; // be
aninvoked from the constructors of the subclasses
uninitialized
depth = -1; // box
System.out.println("Volume of mybox1 is " + vol);
using the keyword super.
}System.out.println("Weight of mybox1 is " + mybox1.weight);
// constructor
The syntax used
System.out.println(); when
to call all dimensions
a superclass’s specified
constructor is:
Box(double w, double h, double d) {
super(),
vol width = w; heightor
= mybox2.volume(); = h;super(parameters);
depth = d;
}System.out.println("Volume of mybox2 is " + vol);
// compute and return volumeof mybox2 is " + mybox2.weight);
System.out.println("Weight
double volume() {
System.out.println();
return
} width * height * depth;
} }
} Object Oriented Programming - CoSc2051 15
Recap
1. What is the output of running the class C in (a)? What problem
arises in compiling the program in (b)?
public class A {
public A() {
System.out.println("A's no-arg constructor is invoked");
}
}
//sub class
class B extends A {}
public class C {
public static void main(String[] args) {
B b = new B();
}
}
Cat
Extends
Animal
Object Oriented Programming - CoSc2051 18
Introduction to Inheritance
Single level Inheritance in Java
class Animal {
Invoid
single inheritance, one class inherits the
eat(){System.out.println(“eating”);} properties of
}
another.
class Dog extends Animal {
Itvoid
enables a derived class to inherit the
bark(){ properties and behavior
System.out.println(“barking”);}
} from a single parent class.
ThisTestInheritance
class will, in turn, enable
{ code reusability as well as add new
public static void main(String args[]) {
features
Dog d = to theDog();
new existing code.
d.bark();
d.eat();
}
Extends
}
Extends Extends
Puppy Dog Animal
class TestInheritance2
Extends { Extends
public
Puppystatic void main(StringDog
args[]) { Animal
Puppy d = new Puppy();
d.weep();
d.bark();
d.eat();
}
}
Object Oriented Programming - CoSc2051 21
Introduction to Inheritance
Recap
RULE 1: Multiple Inheritance is NOT permitted in Java.
RULE 2: Cyclic Inheritance is NOT permitted in Java.
RULE 3: Private members do NOT get inherited.
RULE 4: Constructors cannot be Inherited in Java.
RULE 5: In Java, we assign parent reference to child objects.
RULE 6: Constructors get executed because of super() present
in the constructor.
polymorphism.
It also helps in compile-time
return num1;
} else {
return num2;
}
}
public static int max(int num1, int num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
System.out.println(max(1, 2));
ambiguous
}
public static double max(int num1, double num2) {
invocation
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
}
Object Oriented Programming - CoSc2051 30
Method Overriding and Overloading
Difference between Method Overloading and Method Overriding
//concrete class
class sub extends Super {
Void meth2() {
System.out.println(“meth2”);
}
}
Object Oriented Programming - CoSc2051 40
Abstract Classes
class test {
}
}
Object of an Abstract class cannot be created but object of Concrete class can be created.
Reference of abstract class is allowed.
Example:
Method which is not having a body is known as Abstract method, the method must be declared as
abstract.
The abstract method is undefined method.
A class is Abstract class if at least one of the methods is abstract.
Object Oriented Programming - CoSc2051 41
Abstract Classes
If any other class inherits abstract class then that class also
becomes abstract class but to become a concrete class the subclass
must override the undefined method.
A class becomes useful if it overrides all the methods of abstract
class
Abstract classes are used for imposing standards and sharing
methods
Sub classes are meant for following standards.
interface test1 {
void meth1(); Collection of abstract method
void meth2();
}
class test2 implements test1 {
public void meth1() {}
Implementation of abstract
public void meth2() {} method in derived class
}
class test {
public static void main(String[] args){
test1 t=new test2 (); Creating an object derived class
t. meth1();
} Calling method
}
Object Oriented Programming - CoSc2051 46
Interfaces
Do’s and Don’ts of Interfaces
By default, methods are Public and Abstract.
As methods are to be implemented by the classes, they can’t be
made private.
Identifiers can be used in interfaces but the identifiers must be
given in Upper cases.
Identifiers are by default final and static.
Method inside an interface cannot have body but the method can
have body if the method is static.
Static members can be accessed in main method by using interface
name and dot operator. Object Oriented Programming - CoSc2051 47
Interfaces
Do’s and Don’ts of Interfaces
An interface can be extended from another interface.
Interface VS Multiple
Inheritance
In C++ one class can inherit from multiple classes.
Multiple Inheritance in java is achieved using Interfaces.
Interfaces are perfect than using Multiple Inheritance.
Way of thinking in java is more perfect than C++.