Java Unit 2
Java Unit 2
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).
// 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
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.
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);
}
}
Output:
Rollno = 102689
Name = RATHEESH
Total = 350
Percentage = 70