JavaUNIT 3 (1)
JavaUNIT 3 (1)
3.1 Inheritance
It is the mechanism in Java by which one class is allowed to inherit the features(fields
and methods) of another class.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Java Inheritance Types
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Inheritance
In single inheritance, subclasses inherit the features of one superclass. In the image below,
class A serves as a base class for the derived class B.
import java.io.*;
import java.lang.*;
import java.util.*;
class One {
System.out.println("I");
System.out.println("Love Java");
g.print();
g.print_for();
}
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts
as the base class for other classes.
import java.io.*;
import java.lang.*;
import java.util.*;
class One {
System.out.println("I");
System.out.println("Love");
System.out.println("Java");
}
g.print1();
g.print2();
g.print3();
3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the
below image, class A serves as a base class for the derived classes B, C, and D.
class A {
class B extends A {
class C extends A {
}
class D extends A {
obj_B.print_A();
obj_B.print_B();
obj_C.print_A();
obj_C.print_C();
obj_D.print_A();
obj_D.print_D();
In Multiple inheritances, one class can have more than one superclass and inherit features from all parent
classes. Please note that Java does not support multiple inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces. In the image below, Class C is derived from interfaces A and B.
Syntax
class MyClass implements Interface1, Interface2, Interface3
{
// class body
}
import java.io.*;
import java.lang.*;
import java.util.*;
interface One {
interface Two {
System.out.println("Java");
// Drived class
c.print1();
c.print2();
c.print3();
}
}
5. Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple inheritances
with classes, hybrid inheritance involving multiple inheritance is also not possible with classes. In Java, we can
achieve hybrid inheritance only through Interfaces if we want to involve multiple inheritance to implement
Hybrid inheritance.
Single and Multiple Inheritance (not supported but can be achieved through interface)
Multilevel and Hierarchical Inheritance
Hierarchical and Single Inheritance
Multiple and Multilevel Inheritance
class HumanBody
interface Male
interface Female
{
obj.show();
obj.displayChild();
d1.eat();
a1.eat();
int id;
String name;
Student(){
id = i;
name = n;
//object creation
class A
void m1()
class B extends A
{
// overriding m1()
void m1()
class C extends A
// overriding m1()
void m1()
// Driver class
class Dispatch
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
A ref;
ref.m1();
ref = b;
ref.m1();
ref = c;
ref.m1();
1. Variable
2. Method
3. Class
Final variables: When a variable is declared as final, its value cannot be changed once it has been initialized.
This is useful for declaring constants or other values that should not be modified.
Final methods: When a method is declared as final, it cannot be overridden by a subclass. This is useful for
methods that are part of a class’s public API and should not be modified by subclasses.
Final classes: When a class is declared as final, it cannot be extended by a subclass. This is useful for classes
that are intended to be used as is and should not be modified or extended.
This is used when we want to call the parent class method. So whenever a parent and child class have the same-
named methods then to resolve ambiguity we use the super keyword.
The super keyword can also be used to access the parent class constructor. One more important thing is that
‘super’ can call both parametric as well as non-parametric constructors depending on the situation.
Interface In Java
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not the method body. It is used to achieve abstraction and
multiple inheritances in Java using Interface. In other words, you can say that interfaces can
have abstract methods and variables. It cannot have a method body.
1)First Example
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;
}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Packages In Java
Package in Java is a mechanism to encapsulate a group of classes, sub packages and
interfaces. Packages are used for:
Preventing naming conflicts. For example there can be two classes with name
Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee
Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
Providing controlled access: protected and default have package level access control.
A protected member is accessible by classes in the same package and its subclasses. A
default member (without any access specifier) is accessible by classes in the same
package only.
Packages can be considered as data encapsulation (or data-hiding).
2. Build In Packages:
Built-in Packages These packages consist of a large number of classes which are a part of
Java API.Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces
(like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
1. import package.*;
2. import package.classname;
3. fully qualified name.
eg.
Import java.util.*;
Import java.util.Vector;