Java Notes Unit III
Java Notes Unit III
Father as parent class and Son as child class. Son acquires the properties like color,
a character from Father. Hence, it is called inheritance.
Inheritance in Java is that acquires the properties from one class to another class. A
class can inherit attributes and methods from another class. The main advantage of
inheritance is code reusability.
The class that inherits the properties is known as the sub-class or the child class.
The class from which the properties are inherited is known as the superclass or the
parent class.
Syntax of Inheritance:
class parent_class
{
// parent_class data variables
// parent_class member functions
}
class child_class extends parent_class
{
// child_class data variables
// child_class member functions
}
Types of Inheritance
The different types of Inheritance are:
• Single Inheritance
• Multiple Inheritance
• Multi-Level Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
i) Single Inheritance:
When a class inherits another class, it is known as single inheritance. In the example
given below, the Son class inherits the Father class, so there is single inheritance.
Example:
import java.lang.*;
class Father { // Base class
void work() {
System.out.println("working...");
}
}
class Son extends Father { // Derived class
void play() {
System.out.println("playing...");
}
}
class Inheritance {
public static void main(String args[]) {
Son s = new Son();
s.play(); // inherited from derived class
s.work(); // inherited from base class
}
}
Example:
import java.lang.*;
class Father { // Base class
void work() {
System.out.println("working...");
}
}
class Son extends Father { // Derived class 1
void play() {
System.out.println("playing...");
}
}
class Daughter extends Father { // Derived class 2
void learn() {
System.out.println("learning...");
}
}
class Inheritance{
public static void main(String args[]) {
Son s = new Son();
s.work(); // inherited from base class
s.play(); // inherited from derived class 1
//s.learn(); shows error because it is the method of another derived class
}
}
Interface:-
An Interface in Java programming is defined as an abstract type used to specify the
behavior of a class. A Java interface contains static constants and abstract methods.
An interface can implement multiple interfaces. In Java, interfaces are declared
using the interface keyword.
Example:
import java.lang.*;
interface Father {
public void work();
}
interface Mother {
public void cook();
}
interface Son extends Father, Mother{
public void play();
}
public class Inheritance{
public static void main(String[] args){
Son s = new Son() {
public void work() {
System.out.println("working...");
}
public void cook() {
System.out.println("cooking...");
}
public void play() {
System.out.println("playing...");
}
};
s.work(); // inherited from base class 1
s.cook(); // inherited from base class 2
s.play(); // inherited from derived class
}
}
Java Package:-
A java package is a group of similar types of classes, interfaces, and sub-packages
providing access protection and namespace management. Package in java can be
categorized in two forms, built-in package, and user-defined package.
Advantage of Java Package
• Java package is used to categorize the classes and interfaces so that they can
be easily maintained.
• Java package provides access protection.
• Java package removes naming collision.
Some of the existing packages in Java are −
• java.lang − bundles the fundamental classes.
• java.io − classes for input , output functions are bundled in this package.
• java.util - classes such us vectors, hash tables, random numbers, date.
• java.awt - classes for windows, buttons, lists, menus etc.
• java. net - classes for networking.
• java.applet - classes for creating and implementing applets.
Creating Packages:-
For creating a package simply include a package command followed by name of
the package as the first statement in the java source file.
• Package statement must be first statement in the program even before the
import statement.
• All classes of the package which we wish to access outside the package must
be declared public.
Syntax:
package package_name;
Example:
package emp; // package declaration
public class employee // class declaration
{
String empId;
String name;
}
The above statement will create a package with the name emp in the project
directory. Java uses file system directories to store packages. For example, the .java
file for any class you define to be part of emp package must be stored in
a directory called emp.
Valid and Invalid packages names:
package employee; // ✔️ valid
package EMPLOYEE; // ❌ invalid
Importing a Package:-
To import the java package into a class, we need to use the java import keyword
which is used to access the package and its classes into the java program.
Use import to access built-in and user-defined packages into your java source file
to refer to a class in another package by directly using its name.
syntax:
Example: