Concept
Concept
b) Method Overloading
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
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
1. Encapsulation:
2. Inheritance:
3. Polymorphism:
4. Abstraction:
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.
Polymorphism: Objects of different classes can be treated as if they were of the same type.
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.
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.
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.
Three types:
1. Constructor injection.
2. Property injection.
3. Method injection.
M) Java Main()