OOPs Detailed Notes for Placement Preparation
Class and Object
A class is a blueprint for creating objects. It defines the properties and methods common to all objects of one
type.
Example:
class Student {
String name;
void display() { System.out.println(name); }
An object is an instance of a class:
Student s1 = new Student();
Constructor
Constructors initialize objects. Called automatically at object creation.
- Default Constructor:
class A {
A() { System.out.println("Default constructor"); }
- Parameterized Constructor:
A(int x) { this.x = x; }
- Copy Constructor (manual in Java):
A(A obj) { this.x = obj.x; }
new Keyword
Used to allocate memory dynamically and call the constructor.
Example: Student s = new Student();
Abstraction
Abstraction hides internal details and shows only essential features.
OOPs Detailed Notes for Placement Preparation
Implemented using Abstract Class and Interface.
Abstract Class and Abstract Method
An abstract class contains at least one abstract method and cannot be instantiated.
Example:
abstract class Animal {
abstract void sound();
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
Interface and Multiple Inheritance
Interface contains abstract methods. Variables are public static final by default.
Used to achieve total abstraction and multiple inheritance.
Example:
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() {}
public void display() {}
Polymorphism
Polymorphism means the ability to take many forms.
- Compile Time (Method Overloading):
class A {
void show() {}
void show(int a) {}
OOPs Detailed Notes for Placement Preparation
- Run Time (Method Overriding):
class Animal {
void sound() {}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
Encapsulation
Wrapping data and code together into a single unit.
Use private variables with public getter/setter methods.
Example:
class Person {
private int age;
public void setAge(int age) { this.age = age; }
Inheritance
One class inherits features from another using `extends`.
Types:
- Single
- Multilevel
- Hierarchical
- Multiple (via interface)
Example:
class A {}
class B extends A {}
super Keyword
OOPs Detailed Notes for Placement Preparation
Refers to parent class members. Used to access parent constructor, methods, or variables.
Example:
class A { A() { System.out.println("A"); } }
class B extends A { B() { super(); } }
this Keyword
Refers to the current class object. Used to resolve naming conflicts.
Example:
class A {
int x;
A(int x) { this.x = x; }
final Keyword
Used to restrict changes.
- final variable: constant
- final method: can't be overridden
- final class: can't be inherited
Example:
final class A {}
final int x = 10;
final void show() {}
Points to Remember
- Abstract class can have both abstract and non-abstract methods.
- Interface methods are public and abstract by default.
- An abstract method must be overridden in the subclass.
- Java does not support multiple inheritance with classes, but supports with interfaces.
- Overloading = Same method name, different parameters.
OOPs Detailed Notes for Placement Preparation
- Overriding = Same method name and parameters, different class with inheritance.
- Encapsulation improves data security.
- `super` and `this` help in inheritance and object context.