No. of Slides:66: Object Oriented Programming
No. of Slides:66: Object Oriented Programming
No. of Slides:66: Object Oriented Programming
TEXT BOOKS:
1. Java: the complete reference, 7th edition,
Herbert schildt, TMH.Understanding
2. OOP with Java, updated edition, T. Budd,
Pearson education.
No. of slides:66
INDEX
UNIT 3 PPT SLIDES
Animal
inheritance is transitive
An instance of class Parrot is also an
instance of Bird, an instance of Animal,
…, and an instance of class Object
L 1.3
Base Class Object
In Java, all classes use inheritance.
L 1.4
Base Class Object (cont)
L 1.5
Base class
1) a class obtains variables and methods from
another class
2) the former is called subclass, the latter super-
class (Base class)
3) a sub-class provides a specialized behavior
with respect to its super-class
4) inheritance facilitates code reuse and avoids
duplication of data
L 1.6
Inheritance is one of the pillars of object-orientation.
A new class is derived from an existing class:
existing class is called super-class
derived class is called sub-class
L 1.7
extends
Is a keyword used to inherit a class from
another class
Allows to extend from only one class
baseobj=subobj;
// now its pointing to sub class
L 1.9
Subclass, Subtype and
Substitutability
A subtype is a class that satisfies the principle of
substitutability.
+ getWeight() : int
Bird
+ fly() : void
L 2.6
Substitutability (Deriving Subclasses)
L 3.2
Forms of Inheritance
(- Inheritance for Specification -)
This is another most common use of inheritance. Two different
mechanisms are provided by Java, interface and abstract, to
make use of subclassification for specification.
Subtype is formed and substitutability is explicitly upheld.
Mostly, not used for refinement of its parent class, but instead is
used for definitions of the properties provided by its parent.
L 3.4
Specification
L 4.2
Forms of Inheritance
(- Inheritance for Extension -)
L 4.3
Generalization or Extension
The child class extends the parent class by
providing more functionality
In some sense, opposite of subclassing for
specialization
The child doesn't change anything inherited from
the parent, it simply adds new features
Often used when we cannot modify existing base
parent class
Example, ColoredWindow inheriting from Window
Add additional data fields
L 4.4
Forms of Inheritance
(- Inheritance for Limitation -)
L 4.5
Limitation
L 4.8
Summary of Forms of Inheritance
• Specialization. The child class is a special case of the parent class; in
other words, the child class is a subtype of the parent class.
• Specification. The parent class defines behavior that is implemented
in the child class but not in the parent class.
• Construction. The child class makes use of the behavior provided by
the parent class, but is not a subtype of the parent class.
• Extension. The child class adds new functionality to the parent class,
but does not change any inherited behavior.
• Limitation. The child class restricts the use of some of the behavior
inherited from the parent class.
• Combination. The child class inherits features from more than one
parent class.
L 4.9
The Benefits of Inheritance
Software Reusability (among projects)
Increased Reliability (resulting from reuse and
sharing of well-tested code)
Code Sharing (within a project)
Consistency of Interface (among related objects)
Software Components
Rapid Prototyping (quickly assemble from pre-
existing components)
Polymorphism and Frameworks (high-level
reusable components)
Information Hiding
L 5.1
The Costs of Inheritance
Execution Speed
Program Size
Message-Passing Overhead
Program Complexity (in overuse of inheritance)
L 5.2
Types of inheritance
Acquiring the properties of an existing Object into
newly creating Object to overcome the re-declaration
of properties in deferent classes.
These are 3 types:
1.Simple Inheritance
SUPER SUPER
extends extends
L 5.3
2. Multi Level 3. Multiple
Inheritance Inheritance
SUPER 1
SUPER SUPER 2
implements
extends
SUPER 1 SUPER 2
SUB
SUB
extends implements
extends
L 5.4
Member access rules
Book
protected int pages
+ getPages() : int
+ setPages(): void
Dictionary
+ getDefinitions() : int
+ setDefinitions(): void
+ computeRatios() : double
L 6.3
Example: Super-Class
class A {
int i;
void showi() {
System.out.println("i: " + i);
}
}
L 6.4
Example: Sub-Class
class B extends A {
int j;
void showj() {
System.out.println(“j: " + j);
}
void sum() {
System.out.println("i+j: " + (i+j));
}
}
L 6.5
Example: Testing Class
class SimpleInheritance {
public static void main(String args[]) {
A a = new A();
B b = new B();
a.i = 10;
System.out.println("Contents of a: ");
a.showi();
b.i = 7; b.j = 8;
System.out.println("Contents of b: ");
b.showi(); b.showj();
System.out.println("Sum of I and j in b:");
b.sum();
}
} L 6.6
Multi-Level Class Hierarchy
Box(Box ob) {
width = ob.width;
height = ob.height; depth = ob.depth;
}
double volume() {
return width * height * depth;
}
} L 6.7
Multi-Level Class Hierarchy
BoxWeight(BoxWeight ob) {
super(ob);
weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {
super(w, h, d);
weight = m;
}
} L 6.7
Multi-Level Class Hierarchy
Ship(Ship ob) {
super(ob);
cost = ob.cost;
}
Ship(double w, double h, double d, double m, double c) {
super(w, h, d, m);
cost = c;
}
}
L 6.8
Multi-Level Class Hierarchy
class DemoShip {
double vol;
vol = ship1.volume();
System.out.println("Volume of ship1 is " + vol);
System.out.print("Weight of ship1 is”+ship1.weight);
L 6.9
Multi-Level Class Hierarchy
vol = ship2.volume();
System.out.println("Volume of ship2 is " + vol);
L 6.10
“super” keyword uses:
‘super’ is a keyword used to refer to hidden variables of
super class from sub class.
super.a=a;
L 6.11
Super and Hiding
L 6.12
Example: Super and Hiding
class A {
int i;
}
class B extends A {
int i;
B(int a, int b) {
super.i = a;
i = b;
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
} L 6.13
Example: super and Hiding contd.
Although the i variable in B hides the i variable in
A, super allows access to the hidden variable of
the super-class:
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
L 6.14
Using final with inheritance
final keyword is used declare constants which can not
change its value of definition.
L 6.15
Preventing overriding with final
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() {
System.out.println("Illegal!");
}
}
The class B declaration is illegal (Comilation Error)
L 6.16
Preventing Inheritance with final
class B extends A { … }
L 7.1
Polymorphism
A polymorphic reference can refer to different
types of objects at different times
In Java every reference can be polymorphic
except of references to base types and final
classes.
It is the type of the object being referenced, not
the reference type, that determines which method
is invoked
Polymorphic references are therefore resolved at
run-time, not during compilation; this is called
dynamic binding
L 7.3
Example: Hiding with Overriding 1
class A {
int i, j;
A(int a, int b) {
i = a; j = b;
}
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
L 7.4
Example: Hiding with Overriding 2
class B extends A {
int k;
void show() {
System.out.println("k: " + k);
}
}
L 7.5
Example: Hiding with Overriding 3
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show();
}
}
L 7.7
Abstract Classes
Java allows to define abstract classes
use the modifier abstract on a class header to
declare an abstract class
abstract class Vehicle
{…}
An abstract class is a placeholder in a class
hierarchy that represents a generic concept
Vehicle
L 7.11