You are on page 1of 7

UNIT II Inheritance, Package, Interface, Exception handling

Inheritance-basics, using super keyword, multilevel hierarchy, constructors, method overriding, Dynamic
Method Dispatch, abstract classes, final keyword.
Packages-introduction, access protection, importing packages.
Interfaces-defining and implementing interfaces, applying interfaces, variables in interfaces, Extending
interface.
Exception Handing - fundamentals, exception types, using try and catch, multiple catch clauses, nested try
statements, throw, throws, finally, java’sbuilt-inexception.
What is inheritance?

Inheritance can be defined as the procedure or mechanism of acquiring all the properties and behavior of one
class to another, i.e., acquiring the properties and behavior of child class from the parent class. This concept
was built to achieve the advantage of creating a new class that gets built upon an already existing class(es). It
is mainly used for code reusability within a Java program.

The class that gets inherited taking the properties of another class is the subclass or derived class or child class.
The class whose properties get inherited is the superclass or base class or parent class. The
keyword extends used to inherit the properties of the base class to derived class.
Syntax:
class base
{
.....
.....
}
class derive extends base
{
.....
.....
}
Java supports three types of inheritance. These are:
Single Inheritance
When a single class gets derived from its base class, then this type of inheritance is termed as single
inheritance. In the above figure class A as the base class, and class B gets derived from that base class.
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
super keyword:

The super keyword in Java is used in subclasses to access super class members (attributes, constructors and
methods).

Uses of super keyword


1. To call methods of the superclass that is overridden in the subclass.
2. To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same
name.
3. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.

// Using super
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
This program displays the following:
i in superclass: 1
i in subclass: 2
MULTILEVEL HIERARCHY

Creating multilevel hierarchy in java:

In simple inheritance a subclass or derived class derives the properties from its parent class, but in multilevel
inheritance a subclass is derived from a derived class. One class inherits only single class. Therefore, in
multilevel inheritance, every time ladder increases by one. The lower most class will have the properties of all
the super classes’.
It is common that a class is derived from another derived class. The class student serves as a base class for the
derived class marks, which in turn serves as a base class for the derived class percentage. The class marks is
known as intermediates base class since it provides a link for the inheritance between student and percentage.
The chain is known as inheritance path. When this type of situation occurs, each subclass inherits all of the
features found in all of its super classes. In this case, percentage inherits all aspects of marks and student.

Example for multilevel hierarchy:

class student
{
int rollno;
String name;

student(int r, String n)
{
rollno = r;
name = n;
}
void dispdatas()
{
System.out.println("Rollno = " + rollno);
System.out.println("Name = " + name);
}
}

class marks extends student


{
int total;
marks(int r, String n, int t)
{
super(r,n); //call super class (student) constructor
total = t;
}
void dispdatam()
{
dispdatas(); // call dispdatap of student class
System.out.println("Total = " + total);
}
}

class percentage extends marks


{
int per;

percentage(int r, String n, int t, int p)


{
super(r,n,t); //call super class(marks) constructor
per = p;
}
void dispdatap()
{
dispdatam(); // call dispdatap of marks class
System.out.println("Percentage = " + per);
}
}
class Multi_Inhe
{
public static void main(String args[])
{
percentage stu = new percentage(102689, "RATHEESH", 350, 70); //call constructor percentage
stu.dispdatap(); // call dispdatap of percentage class
}
}

Output:
Rollno = 102689
Name = RATHEESH
Total = 350
Percentage = 70

When Constructors Are Called


When a class hierarchy is created, in what order are the constructors for the classes that
make up the hierarchy called? For example, given a subclass called B and a superclass
called A, is A's constructor called before B's, or vice versa? The answer is that in a class
hierarchy, constructors are called in order of derivation, from superclass to subclass.
Further, since super( ) must be the first statement executed in a subclass' constructor,
this order is the same whether or not super( ) is used. If super( ) is not used, then the
default or parameterless constructor of each superclass will be executed. The following
program illustrates when constructors are executed:
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
output:
Inside A's constructor
Inside B's constructor
Inside C's constructor
Note: the constructors are called in order of derivation

You might also like