Java Merged
Java Merged
Single Inheritance:
Single level inheritance is a type of inheritance in which a child class inherits directly from a
single parent class.
It allows the child class to reuse the fields and methods of the parent class.
Syntax:
class ParentClass {
// Parent class code
}
class ChildClass extends ParentClass {
// Child class code
}
[Link] 1/16
7/23/25, 3:47 PM Types of Inheritance in Java
Program example:
// Parent class
class BankAccount
{
void accountType()
{
[Link]("This is a general bank account.");
}
}
// Child class
class SavingsAccount extends BankAccount
{
void interestRate()
{
[Link]("Savings account offers 4% interest.");
}
}
// Main class
public class MainApp
{
public static void main(String[] args)
{
SavingsAccount sa = new SavingsAccount();
[Link](); // Inherited method
[Link](); // Specific method
} }
Output:
This is a general bank account.
Savings account offers 4% interest.
Multilevel Inheritance:
Multilevel inheritance is a type of inheritance where a class inherits from a child class,
making a chain of inheritance.
Here, a class acts as a parent for another class, which in turn acts as a parent for a third
class.
This forms a hierarchy of classes connected through inheritance.
Syntax:
class Grandparent {
// code
}
class Parent extends Grandparent {
// code
}
class Child extends Parent {
// code
}
[Link] 2/16
7/23/25, 3:47 PM Types of Inheritance in Java
Program example:
// Grandparent class
class Animal
{
void eat()
{
[Link]("Animal is eating.");
}
}
// Parent class
class Dog extends Animal
{
void bark()
{
[Link]("Dog is barking.");
}
}
// Child class
class Puppy extends Dog
{
void weep()
{
[Link]("Puppy is weeping.");
}
}
public class MainApp
{
public static void main(String[] args)
{
Puppy p = new Puppy();
[Link](); // From Animal
[Link](); // From Dog
[Link](); // Own method
} }
[Link] 3/16
7/23/25, 3:47 PM Types of Inheritance in Java
Output:
Animal is eating.
Dog is barking.
Puppy is weeping.
Hierarchical Inheritance:
Hierarchical inheritance is a type of inheritance where multiple child classes inherit from a
single parent class.
It allows multiple subclasses to share the properties and behaviors of the same parent class.
This type of inheritance is useful for creating a common base class for multiple classes.
Syntax:
class Parent {
// Parent class code
}
class Child1 extends Parent {
// Child1 specific code
}
class Child2 extends Parent {
// Child2 specific code
}
[Link] 4/16
7/23/25, 3:47 PM Types of Inheritance in Java
Program example:
// Parent class
class Vehicle
{
void fuelType()
{
[Link]("Uses fuel.");
}
}
[Link] 5/16
7/23/25, 3:47 PM Types of Inheritance in Java
Output:
Uses fuel.
Car has 4 wheels.
Uses fuel.
Bike has a handlebar.
Multiple Inheritance:
(commonly known as the "Diamond Problem"). However, it can be achieved using interfaces.
Multiple inheritance refers to a feature where a class can inherit features (methods and
fields) from more than one parent class.
Java does not support multiple inheritance with classes directly, but it supports it through
interfaces.
This prevents conflicts and ambiguity caused by the Diamond Problem.
interface A
{
void methodA();
}
interface B
{
void methodB();
}
class C implements A, B {
public void methodA()
{
[Link]("Method from interface A");
}
[Link] 6/16
7/23/25, 3:47 PM Types of Inheritance in Java
// Interface A
interface A {
void displayA();
}
// Interface B
interface B {
void displayB();
}
Output:
Display from interface A
Display from interface B
Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance (e.g., single, multiple,
multilevel).
It represents a scenario where different inheritance types are combined to form a complex
hierarchy.
Java does not support hybrid inheritance with classes due to ambiguity issues but supports
it through interfaces.
Syntax using interfaces and a class
interface A {
void methodA();
}
interface B {
void methodB();
}
class C {
void methodC()
{
[Link]("Method from class C");
}
}
// Class D inherits class C and implements A and B
class D extends C implements A, B {
public void methodA() {
[Link]("Method from interface A");
}
[Link] 8/16
7/23/25, 3:47 PM Types of Inheritance in Java
// Interface A
interface A{
void showA();
}
// Interface B
interface B{
void showB();
}
// Class C
class C {
void showC() {
[Link]("Show from class C");
}
}
// Class D: extends C and implements A and B
class D extends C implements A, B
{
public void showA() {
[Link]("Show from interface A");
}
public void showB() {
[Link]("Show from interface B");
} }
// Main class
public class MainApp
{
public static void main(String[] args) {
C obj = new D();
[Link]();
[Link]();
[Link]();
}
}
[Link] 9/16
7/23/25, 3:47 PM Types of Inheritance in Java
Output:
Show from interface A
Show from interface B
Show from class C
Next Topic
[Link] 10/16
7/23/25, 3:35 PM Inheritance in Java
×
Inheritance in Java
Introduction
Inheritance means acquiring the properties and behaviors of a parent class in a child class.
It allows a subclass (child class) to inherit fields and methods from a superclass (parent
class), promoting code reuse and method overriding.
class Vehicle {
void start()
{
[Link]("Vehicle starts.");
}
}
}
}
[Link] 1/9
7/23/25, 3:35 PM Inheritance in Java
interface Animal
{
void eat();
}
class Dog implements Animal
{
public void eat()
{
[Link]("Dog eats.");
}
}
public class MainApp
{
public static void main(String[] args)
{
Dog myDog = new Dog();
[Link](); // inherited from Animal
Advantages of Inheritance
Code Reusability: Inheritance allows a child class to reuse the code of its parent class.
Easy Maintenance: Changes made in the parent class automatically propagate to child
classes, making maintenance easier.
Method Overriding: Inheritance enables method overriding, allowing a child class to
provide a specific implementation of a method already defined in its parent class.
Polymorphism: Inheritance supports runtime polymorphism using method overriding.
Disadvantages of Inheritance
Tight Coupling: Inheritance creates a tight coupling between parent and child classes, if we
change the parent class, it may affect all child classes.
Increased Complexity: Inheritance can lead to complex class hierarchies, making the code
harder to understand and maintain.
[Link] 2/9
7/23/25, 3:35 PM Inheritance in Java
Types of Inheritance
There are 5 types of inheritance in Java:
1. Single Inheritance: One class inherits the properties and behaviors of one parent class.
2. Multilevel Inheritance: One class inherits the properties and behaviors of a parent class,
and that class is inherited by another class.
3. Hierarchical Inheritance: Multiple classes inherit the properties and behaviors of a single
parent class.
4. Multiple Inheritance: One class inherits the properties and behaviors of multiple classes.
(Not supported in Java directly, but can be achieved using interfaces.)
5. Hybrid Inheritance: A combination of two or more types of inheritance. (Not supported in
Java directly, but can be achieved using interfaces.)
Click Here to learn more about the types of inheritance in Java.
Important Points
Some important points to remember about inheritance in Java:
Java does not support multiple and hybrid inheritance with classes to avoid ambiguity,
such as the diamond problem.
A class can extend only one class, which is known as single inheritance.
Constructors and private members of the parent class are not inherited by the child
class.
A class can implement multiple interfaces, which is Java's way of achieving multiple
inheritance.
The super keyword is used to refer to the parent class, such as accessing parent class
methods or constructors.
The this keyword is used to refer to the current class instance, commonly used to
differentiate between instance variables and parameters.
Encapsulation in Java
Introduction
Encapsulation is the mechanism of binding data (variables) and actions (methods) into a
single unit, called a class.
Technically, every class is an example of encapsulation.
[Link] 3/9
7/23/25, 3:34 PM Encapsulation in Java
Next Topic
class Car
{
// Data members (variables)
String brand;
int speed;
// Calling method
[Link]("Tata", 100);
}
}
[Link] 4/12
7/23/25, 3:34 PM Encapsulation in Java
OUTPUT:
Brand : Tata
Speed : 100
Explanation:
Person class contains both data ( name , age ) and method ( displayInfo() ).
Everything is inside one unit (class) — this is the essence of encapsulation.
Note:
class Car
{
// Private data members (encapsulated)
private String brand;
private int speed;
[Link] 5/12
7/23/25, 3:34 PM Encapsulation in Java
Output:
Speed : Tata
Speed : 100
Use of Encapsulation :-
1. Protects data: Hides data from direct access using private variables.
2. Controls data access: Provides controlled access through public getters and setters.
3. Allows data validation: Enables validation before updating variables (e.g., checking valid
input).
4. Improves code maintainability: Keeps internal implementation hidden, making changes
easier.
5. Enhances flexibility: Internal logic can change without affecting external code.
6. Prevents unauthorized or accidental modifications: Limits who and how data can be
changed.
[Link] 6/12
7/23/25, 3:33 PM Abstraction in Java
Abstraction in Java
Introduction
Abstraction is a concept of :
hiding internal implementation details and howing only the essential features to the
user.
Abstract Class :-
Introduction
An abstract class in Java is a class that is declared using the abstract keyword.
It can contain a mix of abstract methods (without body) and concrete methods (with
body).
It cannot be instantiated (you cannot create objects of it).
[Link] 1/11
7/23/25, 3:33 PM Abstraction in Java
// concrete method
void sleep() {
[Link]("Sleeping...");
}
}
Example:
// Concrete method
void fuelType()
{
[Link]("This car uses petrol or diesel.");
}
}
class Sedan extends Car
{
@Override
void startEngine()
{
[Link]("Sedan engine started with key ignition.");
}
}
[Link] 2/11
7/23/25, 3:33 PM Abstraction in Java
class Car
{
int no_of_tyres = 4;
void displayTyres()
{
[Link]("Car has " + no_of_tyres + " tyres.");
}
void start(){
[Link]("Car starts with a key ignition.");
}
}
void displayTyres() {
[Link]("Scooter has " + no_of_tyres + " tyres.");
}
void start() {
[Link]("Scooter starts with a kick or self-start.");
}
}
{
public static void main(String[] args)
{
Car myCar = new Car();
[Link]();
[Link]();
[Link]();
[Link] 3/11
7/23/25, 3:33 PM Abstraction in Java
Output:
Car has 4 tyres.
Car starts with a key ignition.
[Link] 4/11
7/23/25, 3:33 PM Abstraction in Java
// Abstract class used to remove code duplication and enforce method structure
abstract class Vehicle
{
int no_of_tyres;
// Car class extends abstract class and provides its own implementation
class Car extends Vehicle
{
Car()
{
no_of_tyres = 4;
}
@Override
void start()
{
[Link]("Scooter starts with kick or self-start.");
}
}
[Link] 5/11
7/23/25, 3:33 PM Abstraction in Java
{
public static void main(String[] args)
{
// Using polymorphism (removes disadvantage #1)
Vehicle myVehicle1 = new Car();
[Link]();
[Link]();
[Link]();
// Easier to scale and add new vehicle types consistently (removes disadvant
}
}
Output:
This vehicle has 4 tyres.
Car starts with key ignition.
The JVM determines which method to execute at runtime, based on the object type (not the reference
type).
[Link] 6/11
7/23/25, 3:32 PM Method Overriding in Java
Example :-
class Bank {
double getInterestRate()
{
return 0.0;
}
}
[Link] 1/9
7/23/25, 3:32 PM Method Overriding in Java
Output:
SBI Interest Rate: 6.5%
HDFC Interest Rate: 7.0%
ICICI Interest Rate: 6.8%
Important Points
Some important points to remember about method overriding in Java:
1. Method overriding requires the subclass method to have the same name, return type,
and parameter list as the superclass method.
2. The overriding method must be in a subclass, not in the same class.
3. The access modifier of the overriding method cannot be more restrictive than that of the
overridden method.
(For example, if the parent method is public, the overriding method cannot be private.)
// Valid override
protected void display() {}
[Link] 2/9
7/23/25, 3:32 PM Method Overriding in Java
@Override
void methodName() {
// implementation
}
Example :-
class Calculator {
int add(int a, int b)
{
return a + b;
}
[Link] 3/9
7/23/25, 3:31 PM Method Overloading in Java
Output:
Result of add(int, int): 30
Result of add(double, double): 10.0
Result of add(int, int, int): 6
Note :
Important Points
Some important points to remember about method overloading in Java:
1. Method overloading does not depend on return type, it depends only on the parameter
list.
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; } //Valid overload
[Link] 4/9
7/23/25, 3:31 PM Polymorphism in Java
class Student
{
Student() {}
Student(String name) {}
Student(String name, int age) {}
}
Polymorphism in Java
Introduction
Polymorphism is one of the main concepts of Object-Oriented Programming (OOP) in Java.
Poly → many, Morph → forms; so Polymorphism means "many forms".
It means the ability of a single entity (method, object, or operator) to behave in multiple
ways.
Java uses polymorphism to let us write flexible and reusable code.
Real-world Examples :-
A person: acts as a teacher, father, son — different roles.
Water: takes shape of the container it’s in.
Sound: same sound word used in different tones.
Advantage of Polymorphism :-
Increases flexibility and reusability.
Allows code extensibility without modifying existing code.
Supports single task, multiple implementations.
Enhances maintainability and scalability.
[Link] 1/8
7/23/25, 3:31 PM Polymorphism in Java
class Calculator
{
void add(int a, int b)
{
[Link](a+b);
}
Here:
Calculator calc = new Calculator(); // Reference type = Calculator
[Link](5, 10); // method signature = add(int, int) → decided at compile-time
Runtime Polymorphism
It is also known as Dynamic Binding or Late Binding.
In runtime polymorphism, the JVM (Java Virtual Machine) decides at runtime which
overridden method to invoke based on the actual object (not the reference type).
class Animal {
void makeSound() {
[Link]("Some generic sound");
}
}
[Link] 2/8
7/23/25, 3:31 PM Polymorphism in Java
Here:
Animal obj = new Dog(); // Reference type = Animal, Object type = Dog
[Link](); // Method decided at runtime → calls Dog's makeSound()
[Link] 3/8
7/23/25, 3:31 PM Polymorphism in Java
[Link] 4/8