UNIT 3
Inheritance, packages and Interface:
Inheritance types -Method overriding - Abstract Classes- Packages-Interfaces
Inheritance in Java
1. What is Inheritance?
Definition: Inheritance is a mechanism in Java where one class (child/subclass) can
use the properties and methods of another class (parent/superclass).
Purpose:
o Code reusability
o Reduces redundancy
o Improves maintainability
Syntax:
class Parent {
// parent properties and methods
}
class Child extends Parent {
// child properties and methods
}
2. Types of Inheritance in Java
Java supports these inheritance types:
(a) Single Inheritance
One parent → One child
Simple and common in programming.
Example:
class Vehicle {
void start() {
[Link]("Vehicle starts");
}
}
class Car extends Vehicle {
void honk() {
[Link]("Car honks");
}
}
public class SingleInheritanceExample {
public static void main(String[] args) {
Car myCar = new Car();
[Link](); // From Vehicle
[Link](); // From Car
}
}
(b) Multilevel Inheritance
Chain of inheritance (Parent → Child → Grandchild)
Example:
class Electrical {
void supplyPower() {
[Link]("Supplying electrical power");
}
}
class Motor extends Electrical {
void rotate() {
[Link]("Motor is rotating");
}
}
class Pump extends Motor {
void pumpWater() {
[Link]("Pumping water");
}
}
public class MultilevelExample {
public static void main(String[] args) {
Pump p = new Pump();
[Link]();
[Link]();
[Link]();
}
}
(c) Hierarchical Inheritance
One parent → Multiple children
Each child inherits from the same parent.
Example:
class Sensor {
void readValue() {
[Link]("Reading sensor value");
}
}
class TemperatureSensor extends Sensor {
void showTemperature() {
[Link]("Temperature: 28°C");
}
}
class PressureSensor extends Sensor {
void showPressure() {
[Link]("Pressure: 1.2 bar");
}
}
public class HierarchicalExample {
public static void main(String[] args) {
TemperatureSensor t = new TemperatureSensor();
[Link]();
[Link]();
PressureSensor p = new PressureSensor();
[Link]();
[Link]();
}
}
(d) Multiple Inheritance (through Interfaces only)
Java does not support multiple inheritance using classes (to avoid ambiguity) but
supports it using interfaces.
3. Method Overriding
Definition:
Method overriding happens when a subclass provides its own version of a method
already present in the parent class.
Rules:
1. Same method name
2. Same parameter list
3. Same return type (o
4. r covariant)
5. Access modifier can be more visible, not less.
6. Can’t override final or static methods.
7. Use @Override annotation for clarity.
Example 1: Simple Method Overriding
class Machine {
void operate() {
[Link]("Machine is operating");
}
}
class LatheMachine extends Machine {
@Override
void operate() {
[Link]("Lathe machine is cutting metal");
}
}
public class OverrideExample {
public static void main(String[] args) {
Machine m = new LatheMachine(); // Polymorphism
[Link]();
}
}
Output:
Lathe machine is cutting metal
Example 2: Using super to call parent method
class Device {
void start() {
[Link]("Device is starting");
}
}
class Oscilloscope extends Device {
@Override
void start() {
[Link](); // Call parent version
[Link]("Oscilloscope is now measuring signals");
}
}
public class SuperExample {
public static void main(String[] args) {
Oscilloscope o = new Oscilloscope();
[Link]();
}
}
Output:
Device is starting
Oscilloscope is now measuring signals
4. Summary Table
Type of Inheritance Structure Example Use Case
Single A→B Vehicle → Car
Multilevel A→B→C Electrical → Motor → Pump
Sensor → TemperatureSensor,
Hierarchical A → B, A → C
PressureSensor
Multiple (via Interface1 + Interface2 → Multiple capabilities in one device
Type of Inheritance Structure Example Use Case
Interface) Class
Simple Java Interface Example
// Step 1: Create an interface
interface Machine {
void start();
// Step 2: Create a class that implements the interface
class Motor implements Machine {
public void start() {
[Link]("Motor is starting...");
// Step 3: Main program
public class InterfaceSimple {
public static void main(String[] args) {
Machine m = new Motor(); // Interface reference, Motor object
[Link]();
Simple Multiple Inheritance Example
// First interface
interface Sensor {
void readValue();
}
// Second interface
interface Display {
void showData();
// Class implementing both interfaces
class SmartMeter implements Sensor, Display {
public void readValue() {
[Link]("Reading energy consumption value...");
public void showData() {
[Link]("Displaying data on screen...");
// Main program
public class MultipleInheritanceSimple {
public static void main(String[] args) {
SmartMeter sm = new SmartMeter();
[Link]();
[Link]();
Packages in Java
Definition
A package in Java is a way of grouping related classes, interfaces, and sub-
packages together.
Think of it like a folder in a computer that contains related files.
Packages make code easier to organize, reuse, and maintain.
Types of Packages
1. Built-in Packages
Already available in Java (predefined).
Examples:
[Link] → contains basic classes (String, Math, Object, etc.)
[Link] → contains utility classes (Scanner, ArrayList, HashMap,
etc.)
[Link] → for input/output (File, BufferedReader, etc.)
User-defined Packages
Created by the programmer to organize their own classes.
Advantages of Packages
Code Reusability – Once created, classes in a package can be reused anywhere.
Code Maintenance – Easy to maintain related classes in one place.
Avoid Name Conflicts – Two classes with the same name can exist in different
packages.
Access Protection – Classes and methods can be controlled using access modifiers
(public, protected, etc.).
1.
Syntax to Create a Package
package packagename;
Must be the first statement in the Java file.
Save the file in a folder with the same package name.
Small Example 1 – Built-in Package
import [Link]; // importing built-in package
class BuiltInPackageExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]); // using Scanner class
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name);
}
}
👉 Here, [Link] is the package, and Scanner is a class inside it.
Small Example 2 – User-defined Package
Step 1: Create a Package
File: mypack/[Link]
package mypack; // defining package
public class MyClass {
public void displayMessage() {
[Link]("Hello! This is a user-defined package
example.");
}
}
Step 2: Use the Package
File: [Link]
import [Link]; // importing user-defined package
class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
}
}
👉 Compile and Run:
javac -d . [Link]
javac [Link]
java Main
Simple Real-Life Analogy
Packages = Toolboxes
Each toolbox contains related tools.
Instead of keeping all tools in one big box, we organize them by type (electric,
mechanical, plumbing)
Similarly, Java packages organize related classes together.