a) Polymorphism =Many + Forms
Polymorphism is a concept in object-oriented
programming (OOP) where objects of different classes
can be treated as if they were of the same type. (or) If
one task is performed in different ways, it is known as
Polymorphism.
It allows for flexible and extensible code by enabling
different objects to respond to the same message in
different ways.
We use method Overloading and method Overriding to achieve Polymorphism.
There are two types of Polymorphism:
1. Compile Time Polymorphism
2. Runtime Polymorphism
b) Method Overloading
Method overloading occurs when two or more
methods in the same class have the same name
but different parameters (number, type, or
order).
The compiler determines which method to call
based on the arguments passed to it.
Example:
Java
class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
c) Method Overriding
Method overriding subclass (child class) has the same
method as declared in the parent class. (Or) If a subclass
provides the specific implementation of the method
that has been declared by one of its parent class.
The overridden method must have the same signature
(name, return type, and parameters) as the original
method.
Example:
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}}
d) Pillars of OOP and Real-Life Examples
The four pillars of OOP are:
1. Encapsulation:
o Definition: Bundling data and methods that
operate on that data into a single unit
(class).
o Example: A car object encapsulates
properties like color, make, model, and
methods like start(), stop(), accelerate().
2. Inheritance:
o Definition: Creating new classes (subclasses)
based on existing classes (superclasses),
inheriting their properties and methods.
o Example: A cat class can inherit properties
and methods from an Animal class.
3. Polymorphism:
o Definition: Objects of different classes can be
treated as if they were of the same type.
o Example: A list of animals can contain objects of
different animal classes (dog, cat, bird), and all can
be treated as animals.
4. Abstraction:
o Definition: Focusing on the essential features of
an object while ignoring unnecessary details.
(or) Hiding the internal details and showing
functionality.
o Example: A car class can abstract away the
complex mechanics of the engine and provide methods like start() and drive().
e) OOP Concepts in Java
Java is a pure object-oriented programming language, meaning everything in Java is an object, except for primitive
data types. OOP concepts in Java include:
Classes and Objects: Classes are blueprints for objects, and objects are instances of classes.
Inheritance: Subclasses inherit properties and methods from superclasses.
Polymorphism: Objects of different classes can be treated as if they were of the same type.
Encapsulation: Data and methods are bundled together within classes.
Abstraction: Focusing on essential features while ignoring unnecessary details.
f) Polymorphism and Inheritance
Polymorphism is the ability of objects of different classes to be treated as if they were of the same type.
Inheritance is the creation of new classes based on existing classes. Polymorphism often relies on inheritance
to achieve its effects.
For example, if a subclass overrides a method from its superclass, objects of both classes can be treated as if they
were of the same type, and the appropriate method will be called based on the object's actual type.
g) Class and Functions in OOP
A class is a blueprint for creating objects. It defines the properties
(attributes) and behaviors (methods) that objects of that class will
have.
A function (or method) is a block of code that performs a specific task.
It can be defined within a class or outside of a class.
h) Why Java is Not Pure OOP Based Programming Language
While Java is heavily object-oriented, it is not considered a purely object-oriented language due to the
presence of primitive data types.
These are not objects but rather fundamental data types like int, double, char, etc.
However, Java provides wrapper classes for primitive data types (e.g., Integer, Double, Character) that can be
treated as objects.
i) Can Abstract Class Contain Static Method?
Yes, an abstract class can contain static methods. Static methods belong to the class itself, not to individual objects,
and can be accessed directly without creating an instance of the class.
J) What is Dependency Injection
Dependency Injection (DI) is a design pattern that involves passing
dependencies (objects that an object needs to function) into an
object rather than the object creating them itself. (OR) A
programming technique where an object or function receives other
objects or functions that it needs from an outside source, instead of
creating them internally.
This promotes loose coupling, testability, and maintainability in
software applications.
Three types:
1. Constructor injection.
2. Property injection.
3. Method injection.
K) Class and Object
L) Constructor and Method
M) Java Main()