0% found this document useful (0 votes)
8 views

Java Notes Unit III

The document discusses different types of inheritance in Java including single, multilevel, hierarchical, and hybrid inheritance. It also covers interfaces, packages, and importing packages.

Uploaded by

aiden.atz78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Notes Unit III

The document discusses different types of inheritance in Java including single, multilevel, hierarchical, and hybrid inheritance. It also covers interfaces, packages, and importing packages.

Uploaded by

aiden.atz78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Inheritance:-

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
}
}

ii) Multilevel Inheritance:


When there is a chain of inheritance, it is known as multilevel inheritance. As you
can see in the example given below, Son class inherits the Father class which again
inherits the Grandfather class, so there is a multilevel inheritance.
Example:
import java.lang.*;
class Grandfather { // Base class
void sleep() {
System.out.println("sleeping...");
}
}
class Father extends Grandfather { // Derived class 1
void work() {
System.out.println("working...");
}
}
class Son extends Father { // Derived class 2
void play() {
System.out.println("playing...");
}
}
class Inheritance{
public static void main(String args[]) {
Son s = new Son();
s.sleep(); // inherited from base class
s.work(); // inherited from derived class 1
s.play(); // inherited from derived class 2
}
}
iii) Hierarchical Inheritance:
When two or more classes inherit a single class, it is known as hierarchical
inheritance. In the example given below, Son and Daughter classes inherit the
Father class, so there is hierarchical inheritance.

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
}
}

iv) Hybrid Inheritance:


A hybrid inheritance is a combination of more than one types of inheritance. In the
example given below, Son and Daughter classes inherit the Father class, so there is
hierarchical inheritance and Father class is inherited from the Grandfather class ,so
it is single inheritance. Hence it is a combination of single inheritance and
hierarchical inheritance.
Example:
import java.lang.*;
class Grandfather {
public void sleep() {
System.out.println("sleeping...");
}
}
class Son extends Father {
public void play() {
System.out.println("playing...");
}
}
class Daughter extends Father {
public void read() {
System.out.println("reading...");
}
}
class Father extends Grandfather {
public void work() {
System.out.println("working...");
}
public static void main(String args[]) {
Father obj = new Father();
obj.work(); // inherited from class Father
obj.sleep();// inherited from class Grandfather
}
}
v) Multiple Inheritance:
When one class extends more than one class then this is called multiple
inheritances. Java doesn’t allow multiple inheritances.

Self Study:--- why java doesn't support multiple inheritance?

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
}
}

Extending Multiple Java Interfaces:-


A Java class can only extend one parent class. Multiple inheritance is not allowed.
Interfaces are not classes, however, and an interface can extend more than one
parent interface.
The extends keyword is used once, and the parent interfaces are declared in a
comma-separated list.
Example:-
public interface Hockey extends Sports, Event,…

Nested interface inside another interface:-


The nested interface that defined inside another interface must be accessed
as OuterInterface.InnerInterface.
Example:-
interface OuterInterface{
void outerMethod();
interface InnerInterface{
void innerMethod();
}
}
class OnlyOuter implements OuterInterface{
public void outerMethod() {
System.out.println("This is OuterInterface method");
}
}
class OnlyInner implements OuterInterface.InnerInterface{
public void innerMethod() {
System.out.println("This is InnerInterface method");
}
}
public class NestedInterfaceExample {
public static void main(String[] args) {
OnlyOuter obj_1 = new OnlyOuter();
OnlyInner obj_2 = new OnlyInner();
obj_1.outerMethod();
obj_2.innerMethod();
}
}

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:

import package.name.ClassName; // To import a certain class only


import package.name.* // To import the whole package

Example:

import java.util.Date; // imports only Date class


import java.io.*; // imports everything inside java.io package
Example for creating and importing java packages:
//save this file by Greet.java
package greeting; // creating package
public class Greet{
public void msg() {
System.out.println("Hello");
}
}

//save this file by Main.java


package Java;
import greeting.*; //importing package
class Main {
public static void main(String args[]) {
Greet g = new Greet();
g.msg();
}
}

You might also like