Access Modifier:
Access modifiers are keywords used in programming to control the visibility (access) of classes,
variables, methods, and constructors. They decide from where a member of a class can be accessed.
Common Types of Access Modifiers:
1. Public – Accessible from anywhere.
2. Private – Accessible only within the same class.
3. Protected – Accessible within the same package and by subclasses.
4. Default (Package-Private) – Accessible only within the same package.
Example (Java)
class Person {
public String name; // accessible everywhere
private int age; // accessible only inside this class
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person();
[Link] = "Ali"; // allowed
// [Link] = 25; // not allowed because age is private
[Link](25);
[Link]([Link]());
}
}
Java Constructor: A constructor in Java is a special method that is automatically called when an
object of a class is created. It is mainly used to initialize objects. Example:
class Student {
String name;
// Constructor
Student() {
name = "Hadi";
}
void display() {
[Link](name);
public static void main(String[] args) {
Student s = new Student(); // constructor called
[Link]();
Java Method: A method in Java is a function defined inside a class that performs a specific task. It is
called explicitly by the programmer. Example:
class Calculator {
// Method
int add(int a, int b) {
return a + b;
public static void main(String[] args) {
Calculator c = new Calculator();
int result = [Link](5, 3); // method call
[Link](result);
Inheritance in Java :Inheritance is a concept of Object-Oriented Programming (OOP) where one class
(child/subclass) inherits the properties and methods of another class (parent/superclass).
class Parent {
// properties and methods
class Child extends Parent {
// additional properties and methods
}
Polymorphism in Java
Polymorphism is an Object-Oriented Programming (OOP) concept that means “one name, many
forms”.
It allows a single entity (method or object) to behave differently in different scenarios.
In Java, polymorphism is mainly of two types:
1. Compile-Time Polymorphism (Method Overloading)
• Happens at compile time.
• Same method name, but different parameters (number or type).
• Also called static polymorphism.
Example
class Calculator {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator c = new Calculator();
[Link]("Sum of 2 = " + [Link](5, 10)); // Calls first method
[Link]("Sum of 3 = " + [Link](5, 10, 15)); // Calls overloaded method
}
}
2. Run-Time Polymorphism (Method Overriding)
• Happens at runtime.
• Child class provides its own version of parent class method.
• Also called dynamic polymorphism.
Example
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Animal a = new Dog(); // Reference of parent, object of child
[Link](); // Calls overridden method (Dog's version)
}
}