Unit 4 Part 1 Inheritance
Unit 4 Part 1 Inheritance
College of Engineering
SE Computer- Division A
Course Name :Principle of Programming Language
Course Code: 210256
Course InCharge: Kainjan M. Sanghavi
Inheritances: member access and inheritance, super class references, Using super,
multilevel hierarchy, constructor call sequence, method overriding, dynamic method
dispatch, abstract classes, Object class.
Packages and Interfaces: defining a package, finding packages and CLASSPATH, access
protection, importing packages,
fundamental, exception types, uncaught exceptions, try, catch, throw, throws, finally,
multiple catch clauses, nested try statements, built-in exceptions, custom exceptions
(creating your own exception sub classes).
Departmentof
Department ofComputer
ComputerEngineering
Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Unit 4
Inheritance, Packages and Exception Handling using Java
Managing I/O: Streams, Byte Streams and Character Streams, Predefined Streams,
Reading console Input, Writing Console Output, Print Writer class.
Departmentof
Department ofComputer
ComputerEngineering
Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
● Inheritance –Definition
● Single Inheritance
● Benefits of inheritance
● Member access rules
● super classes
● Polymorphism
● Method overriding
● Using final with inheritance
● abstract classes and
● Base class object.
Definition
● Inheritance is the process of acquiring the properties by the sub
class ( or derived class or child class) from the super class (or base
class or parent class).
● When a child class(newly defined abstraction) inherits(extends) its
parent class (being inherited abstraction), all the properties and
methods of parent class becomes the member of child class.
● In addition, child class can add new data fields(properties) and
behaviors(methods), and
● It can override methods that are inherited from its parent class.
Syntax:-
Single Inheritance
-Derivation of a class from only one base class is called single inheritance.
//base class: A
class A{
//members of A
}
//Derived class syntax: B
class B extends A{
//members of B
}
Department of Computer Engineering
// Create a superclass. /* The subclass has access to all public members
class A { of its superclass. */
int i, j; subOb.i = 7;
void showij() { subOb.j = 8;
System.out.println("i and j: " + i + " " + j); subOb.k = 9;
}
} System.out.println("Contents of subOb: ");
// Create a subclass by extending class A.
class B extends A { subOb.showij();
int k; subOb.showk();
System.out.println();
void showk() {
System.out.println("k: " + k); System.out.println("Sum of i, j and k in subOb:");
}
void sum() { subOb.sum();
System.out.println("i+j+k: " + (i+j+k)); }
} }
} Contents of superOb:
class SimpleInheritance { i and j: 10 20
public static void main(String args[]) {
A superOb = new A(); Contents of subOb:
B subOb = new B();
i and j: 7 8
// The superclass may be used by itself.
k: 9
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of Sum of i, j and k in subOb:
superOb:"); i+j+k: 24
SNJB’s Late Sau. K. B. J. College of Engineering
The Benefits of Inheritance
● Software Reusability ( among projects )
○ Code ( class/package ) can be reused among the projects.
○ Ex., code to insert a new element into a table can be written once
and reused.
● Code Sharing ( within a project )
○ It occurs when two or more classes inherit from a single parent
class.
○ This code needs to be written only once and will contribute only
once to the size of the resulting program.
● Increased Reliability (resulting from reuse and sharing of code)
○ When the same components are used in two or more applications,
the bugs can be discovered more quickly.
A
X
B
A B C
Types of Inheritance
Multilevel Inheritance Multiple Inheritance / Not
Actually exists in Java
A A B
C C
} OK
}
WRONG
WRONG
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
Super Keyword
⚫ Subclass refers to its immediate superclass by using super
keyword.
⚫ super has two general forms.
○ First it calls the superclass constructor.
○ Second is used to access a member of the superclass that has
been hidden by a member of a subclass.
⚫ Using super to call superclass constructors
○ super (parameter-list);
○ parameter-list specifies any parameters needed by the
constructor in the superclass.
○ super( ) must always be the first statement executed inside a
subclass constructor.
Output:
Box() in super class
BoxWeight() in sub class
//Using super to call superclass //constructors
class Box {
Box() {
System.out.println("Box() in super class");
}
Box(int a){
System.out.println("Box(int a) in super class"); }
}
class BoxWeight extends Box {
BoxWeight(){
super(10);
System.out.println("BoxWeight() in sub class"); }
}
class DemoBoxWeight{
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight();
}
}
Output:
Box(int a) in super class
BoxWeight() in sub class
SNJB’s Late Sau. K. B. J. College of Engineering
System.out.println(“k=:”+k);
SNJB’s Late Sau. K. B. J. College of Engineering
class FinalDemo{
public static void main(String sree[]){
final int i=20;
System.out.println(i);
//i=i+1; can’t assign a value to final variable i
//System.out.println(i); cannot assign a value to final
variable i
}
} Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering
2 To prevent overriding
}
Output:
}
Class of Object obj is : java.lang.String
366712642