PROGRAMMING IN JAVA
UNIT II
TOPIC – FEATURES OF OOPS
Department of Computer Science and Engineering
IET DDU
Gorakhpur, India
1
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Outline
INTRODUCTION
Abstraction
Polymorphism
Encapsulation
Inheritance
Method overloading and overriding
14-03-2026 Side 2
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Abstraction
// Define the abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void makeSound();
// Regular method
public void sleep() {
[Link]("Zzz...");
}
}
// Subclass (inherited from Animal)
class Dog extends Animal {
@Override
public void makeSound() {
[Link]("Woof");
}
14-03-2026 Side 3
Cont…
} Animal dog = new Dog();
// Subclass (inherited from Animal) Animal cat = new Cat();
class Cat extends Animal {
@Override [Link]();
public void makeSound() { [Link]();
[Link]("Meow");
} [Link]();
} [Link]();
// Use the abstract class }
class Main { }
public static void main(String[] args) {
Polymorphism
[Link]("Drawing a rectangle");
class Shape { }
public void draw() { }public class PolymorphismExample {
[Link]("Drawing a shape"); public static void main(String[] args) {
} Shape shape1 = new Circle();
}class Circle extends Shape { Shape shape2 = new Rectangle();
@Override [Link](); // Calls the draw() method of
Circle
public void draw() { [Link](); // Calls the draw() method of
[Link]("Drawing a circle"); Rectangle
} }
}class Rectangle extends Shape { }
@Override
public void draw() {
Encapsulation
public class Person {
public int getAge() {
private String name;
return age;
private int age; // Constructor
} // Setter for age
public Person(String name, int age) {
public void setAge(int age) {
[Link] = name;
if (age > 0) {
[Link] = age;
[Link] = age;
} // Getter for name
} else {
public String getName() {
[Link]("Age cannot be
return name;
negative.");
} // Setter for name
}
public void setName(String name) {
} // Display person information
[Link] = name;
public void displayInfo() {
} // Getter for age
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Encapsulation cont…
[Link]("Name: " + name);
[Link]("Age: " + age);
} public static void main(String[] args) {
// Create a Person object
Person person = new Person("Alice", 30); // Access and modify fields using getters
and setters
[Link]("Bob");
[Link](25); // Display person information
[Link]();
}
}
14-03-2026 Side 7
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Inheritance
Method Overriding in Java Inheritance
When you redefine any base class method with the same signature i.e.
same return type, number, and type of parameters in the derived class, it is
known as method overriding in Java. In this case, the method in the
subclass overrides the method in the superclass.
14-03-2026 Side 8
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
class Iet { [Link]("Welcome to
public void print() { // tech");
overridden function }
}
[Link]("Welcome to public class Main {
Iet");
public static void main(String[]
} args) {
} tech obj1 = new tech();
class tech extends Iet { [Link]();
@Override }
public void print() { }
// overriding function
14-03-2026 Side 9
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Super Keyword in Java Inheritance
In the above topic we saw that when the same method is defined in both the
base class and the derived class, the method in the subclass overrides the
one in the superclass. In such cases, the super keyword in Java is used to
call the method of the parent class from the method of the child class.
Code:
class DotNetTricks {
public void print() { // overridden function
[Link]("Welcome to DotNetTricks");
}
}
class ScholarHat extends DotNetTricks {
14-03-2026 Side 10
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
@Override
public void print() { // overriding function
// call method of the superclass
[Link]();
[Link]("Welcome to ScholarHat");
}
}
public class Main {
public static void main(String[] args) {
ScholarHat obj1 = new ScholarHat();
// Call the print() function
[Link]();
}
}
14-03-2026 Side 11
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Single Inheritance in Java }
class TestInheritance
class Animal {
{ public static void main(String args[])
voidsleep(){ {
[Link]("sleeping...");} Dog d=new Dog();
} [Link]();
class Dog extends Animal [Link]();
{ }
voidbark(){ }
[Link]("barking...");
}
14-03-2026 Side 12
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Multilevel Inheritance class TestInheritance2
{
class Animal
{ public static void main(String args[])
void eat(){ {
[Link]("eating...");} BabyDog d=new BabyDog();
} [Link]();
class Dog extends Animal{ [Link]();
void
[Link]();
sleep(){[Link]("sleeping...");}
} }
class BabyDog extends Dog }
{
voidweep(){
[Link]("weeping...");}
}
14-03-2026 Side 13
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Hierarchical Inheritance void meow() {
[Link]("Cat is meowing");
}
class Animal {
}
void eat() {
public class Main {
[Link]("Animal is eating");
public static void main(String[] args) {
}
Dog dog = new Dog();
}
[Link](); // Inherited from Animal
// Child class 1 inheriting from Animal
[Link](); // Defined in Dog class
class Dog extends Animal {
void bark() {
Cat cat = new Cat();
[Link]("Dog is barking");
[Link](); // Inherited from Animal
}
[Link](); // Defined in Cat class
}
}
// Child class 2 inheriting from Animal
}
class Cat extends Animal {
14-03-2026 Side 14
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Multiple Inheritance (Through public void sleep() {
Interfaces) in Java [Link]("Sparrow is sleeping.");
@Override
// Define two interfaces public void fly() {
interface Animal { [Link]("Sparrow is flying.");
void eat(); }
void sleep(); }
} // Main class
interface Bird { public class Main {
void fly(); public static void main(String[] args) {
} Sparrow sparrow = new Sparrow();
// Create a class that implements both [Link](); // Calls the eat() method from the
interfaces Animal interface
class Sparrow implements Animal, Bird { [Link](); // Calls the sleep() method from
the Animal interface
@Override
[Link](); // Calls the fly() method from the
public void eat() {
Bird interface
[Link]("Sparrow is
}
eating.");}
}
@Override
14-03-2026 Side 15
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Hybrid Inheritance // Interface defining behavior for domestic animals
interface Domestic {
void play();
class Animal {
}
void eat() {
[Link]("Animal is eating");
// Sub-interface for specific type of domestic animals
}
interface DogBehavior extends Domestic {
}
void guard();
// Child class 1 inheriting from Animal
}
class Dog extends Animal {
// Class implementing Domestic and DogBehavior
void bark() { interfaces
[Link]("Dog is barking"); class DomesticDog implements DogBehavior {
} public void play() {
} [Link]("Domestic dog is playing");
// Child class 2 inheriting from Animal }
class Cat extends Animal { public void guard() {
void meow() {
[Link]("Cat is meowing");
}}
14-03-2026 Side 16
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
[Link]("Domestic dog is guarding");
}
}
public class Main {
public static void main(String[] args) {
DomesticDog dog = new DomesticDog();
[Link](); // Defined in Domestic interface
[Link](); // Defined in DogBehavior interface
}
}
14-03-2026 Side 17
Method overloading and overriding
Method Overloading
Method overloading involves defining multiple methods within the same class that
share the same name but have different parameter lists. This allows a single method
name to perform similar operations with different types or numbers of inputs,
improving code readability and flexibility. It is an example of compile-time
polymorphism (static binding).
• Occurs within a single class (or an inheritance hierarchy).
• Method signature (name and parameters) must be different. The return type can be
the same or different, but changing the return type alone is not enough for
overloading.
• Static and private methods can be overloaded.
Example: A Calculator class can have different add methods for varying parameter
types and counts, such as add(int a, int b), add(int a, int b, int c), and add(double a,
double b)
ALKA 18
public class OverloadDemo {
// Method 1: two int parameters
static int my_Sum(int a, int b) { return a + b; }
// Method 2: three int parameters
static int my_Sum(int a, int b, int c)
{
return a + b + c;
}
public static void main(String[] args)
{
[Link]("my_Sum with 2 int parameters: "
+ my_Sum(4, 6));
[Link]("my_Sum with 3 int parameters: "
+ my_Sum(4, 6, 7));
}}
ALKA 19
OUTPUT
my_Sum with 2 int parameters: 10
my_Sum with 3 int parameters: 17
Method Overriding
Method overriding occurs when a subclass provides its own specific implementation
for a method that is already defined in its superclass. The purpose is to change the
behavior of an inherited method in the child class to suit its specific needs. This is an
example of runtime polymorphism (dynamic binding), as the method to be invoked is
determined by the actual object type at runtime.
• Requires an inheritance (IS-A) relationship between two classes.
• Method signature (name, parameters, and return type as of Java 5+ covariant
return types) must be the same as the superclass method.
ram 20
• Access modifiers cannot be more restrictive in the subclass than in the superclass
(e.g., a protected parent method can be protected or public in the child class, but
not private).
• Static and final methods cannot be overridden.
Example: A Dog class can override the sound() method inherited from an Animal class
to print "Dog barks" instead of "Animal makes a sound". The @Override annotation is
recommended for clarity and to ensure the rules are followed.
Method Overriding is redefining a superclass method in a subclass with the same
signature and a compatible return type. It enables runtime polymorphism, where the
JVM calls the method based on the actual object type.
// Parent class
class Calculator {
// Method to sum two integers
int my_Sum(int a, int b){
[Link]("Parent class sum method:");
return a + b;
ram 21
}
}
// Child class
class AdvancedCalculator extends Calculator{
// Overriding the my_Sum method
@Override int my_Sum(int a, int b){
[Link](
"Child class overridden sum method:");
// Adding custom behavior
return a + b
+ 10; // adds extra 10 for demonstration
} }
// Main class
public class OverrideDemo {
public static void main(String[] args)
{
Calculator calc1 = new Calculator();
[Link]("Result: "
+ calc1.my_Sum(5, 10));
22
AdvancedCalculator calc2 = new AdvancedCalculator();
[Link]("Result: "
+ calc2.my_Sum(5, 10));
// Polymorphism example
Calculator calc3 = new AdvancedCalculator();
[Link]("Result: "
+ calc3.my_Sum(5, 10));
}
}
ram 23
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
14-03-2026 Side 24
Differences between Method Overloading and Method
Overriding
Feature Method Overloading Method Overriding
Redefining a parent class method in
Defining multiple methods with the
the subclass with the same
Definition same name but different
signature and compatible return
parameters in the same class.
type.
To achieve compile-time To achieve runtime polymorphism
Purpose
polymorphism (static binding). (dynamic binding).
Must be different (in number, type, Must be exactly the same as in the
Parameter List
or order). parent class.
Can be same or different, but must Must be same or covariant (subtype
Return Type
not conflict. of parent’s return type).
25
Not required; can occur within the Requires inheritance between
Inheritance
same class. superclass and subclass.
Cannot reduce parent method’s
Access Modifier Can have any access modifier.
access level.
Cannot be overridden if marked
Static / Final Methods Can be overloaded.
static or final.
Binding Time Compile-time binding. Runtime binding.
Overridden method cannot throw
Overloaded methods can declare
Exception Handling broader checked exceptions than
any exceptions.
the parent.
void add(int a, int b) and void Parent: void show() → Child: void
Example
add(double a, double b) show()
26
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
14-03-2026 Side 27