Loading...
Loading...
Phase 1: Technical Theory | Date: 25 July 2026 | Subject: OOP Concepts | Expected Questions: About 3–4
Today you will study Object-Oriented Programming, commonly called OOP.
OOP is a programming approach in which a program is designed using objects. An object combines data and the functions that work on that data.
| Hour | Topics |
|---|---|
| 1 | Class, Object, Encapsulation, Abstraction and Data Hiding |
| 2 | Single, Multiple, Multilevel, Hierarchical and Hybrid Inheritance; Diamond Problem |
| 3 | Polymorphism, Function Overloading, Function Overriding, Virtual Functions and Pure Virtual Functions |
| 4 | Constructors, Destructors, Friend Functions, this Pointer and Exception Handling |
| 5 | Java Basics: JDK, JRE, JVM, Bytecode, Interface, Abstract Class and try-catch-finally |
After completing this material, you will be able to:
this pointer.try-catch-finally.Object-Oriented Programming is a programming method in which a program is organized around objects.
An object represents a real or logical entity, such as:
Each object usually contains:
| State or Data | Behaviour or Function |
|---|---|
| Colour | Start |
| Speed | Stop |
| Fuel level | Accelerate |
| Model | Apply brake |
The basic idea is:
Object = Data + Functions working on that data
OOP helps programmers:
Procedural programming organizes a program mainly around functions and steps.
Object-oriented programming organizes a program mainly around classes and objects.
| Procedural Programming | Object-Oriented Programming |
|---|---|
| Main focus is functions | Main focus is objects |
| Data and functions may be separate | Data and functions are grouped |
| Usually follows a top-down approach | Usually follows a bottom-up approach |
| Data protection may be weaker | Access control can protect data |
| Code reuse is more limited | Inheritance supports code reuse |
| Example: C | Examples: C++, Java |
C++ supports more than one programming style. It supports:
The four main OOP pillars are:
EAIP
A memory sentence is:
Every Animal Inherits Properties
A class is a programmer-defined blueprint or template for creating objects.
It describes:
class Student
{
private:
int rollNumber;
public:
void setRollNumber(int r)
{
rollNumber = r;
}
void showRollNumber()
{
cout << rollNumber;
}
};
| Part | Meaning |
|---|---|
class | Keyword used to define a class |
Student | Name of the class |
rollNumber | Data member |
setRollNumber() | Member function |
showRollNumber() | Member function |
private | Accessible only through permitted class operations |
public | Accessible from outside the class |
Semicolon after } | Required after a C++ class definition |
A data member is a variable declared inside a class.
Examples:
A member function is a function declared as a member of a class. It usually works with the data members of that class.
An object is an instance of a class.
The class is the design. The object is the actual entity created from that design.
Student s1;
Student s2;
Here:
Student is the class.s1 and s2 are objects.Class Object
----- ------
House blueprint ---> Actual house
Car design ---> Actual car
Student form ---> One student record
The dot operator . accesses a public member through an object.
s1.setRollNumber(101);
s1.showRollNumber();
Output:
101
| Basis | Class | Object |
|---|---|---|
| Meaning | Blueprint or template | Instance of a class |
| Nature | Logical definition | Actual usable entity |
| Example | Student | s1 |
| Quantity | One class can define a type | Many objects can be created |
| Memory | Definition alone does not create separate storage for each ordinary instance member | An object receives storage for its instance data |
In C++:
class are private by default.struct are public by default.An access specifier controls where a class member can be accessed.
C++ has three main access specifiers:
privateprotectedpublic| Access Specifier | Same Class | Derived Class | Outside Through Object |
|---|---|---|---|
private | Yes | No direct access | No |
protected | Yes | Yes | No |
public | Yes | Yes | Yes |
class Account
{
private:
double balance;
protected:
int accountType;
public:
void deposit(double amount)
{
balance = balance + amount;
}
};
A derived class is often informally called a child class.
Encapsulation means combining data and the functions that operate on that data into one unit.
In OOP, that unit is usually a class.
┌────────────────────────────┐
│ BankAccount │
│ │
│ Data: balance │
│ Functions: │
│ deposit() │
│ withdraw() │
│ getBalance() │
└────────────────────────────┘
The data and related functions are enclosed inside the class.
class BankAccount
{
private:
double balance;
public:
void deposit(double amount)
{
if (amount > 0)
balance = balance + amount;
}
double getBalance() const
{
return balance;
}
};
A medicine capsule encloses medicine inside one unit. In programming, a class encloses data and functions inside one unit.
Encapsulation is the wrapping of data and functions into a single unit.
Data hiding means restricting direct access to internal data.
It is usually achieved using:
private membersprotected membersclass Employee
{
private:
double salary;
public:
void setSalary(double s)
{
if (s >= 0)
salary = s;
}
double getSalary() const
{
return salary;
}
};
Outside code cannot directly use:
employee.salary = -5000;
because salary is private.
It must use:
employee.setSalary(5000);
The setter can check whether the value is valid.
These terms are related, but they are not exactly the same.
| Encapsulation | Data Hiding |
|---|---|
| Combines data and functions in one unit | Restricts direct access to data |
| Achieved mainly using classes | Achieved mainly using access specifiers |
| Focuses on organization | Focuses on protection |
| A class can encapsulate members | Private members hide data |
Abstraction means showing only essential features and hiding unnecessary implementation details.
The user knows what an operation does but does not need to know exactly how it does it.
When using an ATM, you:
You do not need to understand:
The ATM provides a simple interface while hiding complex internal work.
class Car
{
public:
void start()
{
checkBattery();
supplyFuel();
igniteEngine();
}
private:
void checkBattery()
{
// Internal work
}
void supplyFuel()
{
// Internal work
}
void igniteEngine()
{
// Internal work
}
};
The user calls:
car.start();
The user does not need to call each internal step.
| Encapsulation | Abstraction |
|---|---|
| Wraps data and functions together | Hides unnecessary implementation details |
| Focuses on how members are grouped | Focuses on what the user needs to see |
| Often uses a class | Often uses abstract classes or interfaces |
| Also supports data protection | Reduces visible complexity |
Example: data and methods inside BankAccount | Example: user calls withdraw() without knowing internal steps |
Inheritance allows a new class to receive properties and behaviours from an existing class.
The existing class is called:
The new class is called:
class Animal
{
public:
void eat()
{
cout << "Eating";
}
};
class Dog : public Animal
{
public:
void bark()
{
cout << "Barking";
}
};
A Dog object can use:
eat() inherited from Animalbark() defined in DogInheritance supports code reuse.
Polymorphism means “many forms.”
The same function name or interface can behave differently in different situations.
| Type | Binding Time | Main Examples |
|---|---|---|
| Compile-time polymorphism | During compilation | Function overloading, operator overloading |
| Runtime polymorphism | During program execution | Function overriding through virtual functions |
Polymorphism is explained fully in Section 3.
class Derived : public Base
{
// Members of Derived
};
Here:
Base is the parent class.Derived is the child class.public is the inheritance mode.C++ supports:
| Base Member | Public Inheritance | Protected Inheritance | Private Inheritance |
|---|---|---|---|
public | Remains public | Becomes protected | Becomes private |
protected | Remains protected | Remains protected | Becomes private |
private | Not directly accessible in derived class | Not directly accessible | Not directly accessible |
Private members still belong to the base-class part of a derived object, but the derived class cannot access them directly. It may use suitable public or protected base-class functions.
In single inheritance, one derived class inherits from one base class.
┌──────────┐
│ Animal │
└────┬─────┘
│
v
┌──────────┐
│ Dog │
└──────────┘
class Animal
{
public:
void eat()
{
cout << "Eat";
}
};
class Dog : public Animal
{
public:
void bark()
{
cout << "Bark";
}
};
Animal → Dog
In multiple inheritance, one derived class inherits from two or more base classes.
┌──────────┐ ┌──────────┐
│ Writer │ │ Speaker │
└────┬─────┘ └────┬─────┘
│ │
└────────┬─────────┘
v
┌──────────┐
│ Person │
└──────────┘
class Writer
{
public:
void write()
{
cout << "Writing";
}
};
class Speaker
{
public:
void speak()
{
cout << "Speaking";
}
};
class Person : public Writer, public Speaker
{
};
A Person object can use both write() and speak().
Java does not support multiple inheritance of classes. A Java class cannot extend two classes.
Java supports multiple inheritance of type or behaviour through interfaces.
In multilevel inheritance, a derived class becomes the base class of another class.
┌──────────┐
│ Animal │
└────┬─────┘
│
v
┌──────────┐
│ Mammal │
└────┬─────┘
│
v
┌──────────┐
│ Dog │
└──────────┘
class Animal
{
public:
void eat()
{
cout << "Eat";
}
};
class Mammal : public Animal
{
public:
void walk()
{
cout << "Walk";
}
};
class Dog : public Mammal
{
public:
void bark()
{
cout << "Bark";
}
};
A Dog can use:
eat()walk()bark()Animal → Mammal → Dog
In hierarchical inheritance, two or more derived classes inherit from the same base class.
┌──────────┐
│ Shape │
└────┬─────┘
┌─────────┼─────────┐
│ │ │
v v v
┌────────┐ ┌────────┐ ┌────────┐
│ Circle │ │ Square │ │Triangle│
└────────┘ └────────┘ └────────┘
class Shape
{
public:
void display()
{
cout << "Shape";
}
};
class Circle : public Shape
{
};
class Square : public Shape
{
};
Both Circle and Square inherit from Shape.
Hybrid inheritance is a combination of two or more inheritance types.
For example, it may combine:
┌──────────┐
│ Person │
└────┬─────┘
┌────────┴────────┐
v v
┌──────────┐ ┌──────────┐
│ Student │ │ Employee │
└────┬─────┘ └────┬─────┘
└────────┬─────────┘
v
┌──────────┐
│ Assistant│
└──────────┘
This structure combines hierarchical and multiple inheritance.
| Type | Meaning | Structure |
|---|---|---|
| Single | One parent, one child | A → B |
| Multiple | Multiple parents, one child | A + B → C |
| Multilevel | Inheritance chain | A → B → C |
| Hierarchical | One parent, multiple children | A → B and A → C |
| Hybrid | Combination of inheritance types | Mixed structure |
SMMHH
Memory sentence:
Smart Minds Make Helpful Humans
When a derived object is created:
When the object is destroyed:
class Base
{
public:
Base()
{
cout << "Base ";
}
~Base()
{
cout << "Base destroyed ";
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived ";
}
~Derived()
{
cout << "Derived destroyed ";
}
};
Creation order:
Base → Derived
Destruction order:
Derived → Base
Construct from parent to child. Destroy from child to parent.
Suppose two base classes contain a function with the same name.
class A
{
public:
void show()
{
cout << "A";
}
};
class B
{
public:
void show()
{
cout << "B";
}
};
class C : public A, public B
{
};
Calling this is ambiguous:
C object;
object.show();
The compiler cannot decide whether to call:
A::show()B::show()Use the scope-resolution operator:
object.A::show();
object.B::show();
The diamond problem occurs when two classes inherit from the same base class, and another class inherits from both of them.
┌──────────┐
│ Person │
└────┬─────┘
┌────────┴────────┐
v v
┌──────────┐ ┌──────────┐
│ Student │ │ Employee │
└────┬─────┘ └────┬─────┘
└────────┬─────────┘
v
┌──────────┐
│ Assistant│
└──────────┘
class Person
{
public:
int age;
};
class Student : public Person
{
};
class Employee : public Person
{
};
class Assistant : public Student, public Employee
{
};
An Assistant object receives two Person base subobjects:
StudentEmployeeTherefore, this can be ambiguous:
Assistant a;
a.age = 25;
The compiler cannot decide which Person::age should be used.
Use virtual inheritance in the intermediate classes.
class Person
{
public:
int age;
};
class Student : virtual public Person
{
};
class Employee : virtual public Person
{
};
class Assistant : public Student, public Employee
{
};
Now Assistant contains only one shared Person base-class subobject.
┌──────────┐
│ Person │
│ one copy │
└────┬─────┘
┌────────┴────────┐
v v
┌──────────┐ ┌──────────┐
│ Student │ │ Employee │
└────┬─────┘ └────┬─────┘
└────────┬─────────┘
v
┌──────────┐
│ Assistant│
└──────────┘
The diamond problem in C++ is solved using:
Virtual base classes or virtual inheritance
Do not confuse this with a virtual function. Both use the word virtual, but they solve different problems.
| Virtual Inheritance | Virtual Function |
|---|---|
| Solves duplicate-base problem | Supports runtime polymorphism |
| Used in inheritance declaration | Used in member-function declaration |
Example: class B : virtual public A | Example: virtual void show() |
The word polymorphism comes from:
Therefore:
Polymorphism means many forms.
It allows one function name or interface to represent different behaviours.
The compiler decides which operation to use during compilation.
Examples:
It is also called:
The decision is made while the program is running.
It is achieved using:
It is also called:
| Compile-Time Polymorphism | Runtime Polymorphism |
|---|---|
| Decision occurs during compilation | Decision occurs during execution |
| Usually faster | May have small runtime overhead |
| Uses overloading or templates | Uses overriding and virtual functions |
| Early or static binding | Late or dynamic binding |
| Inheritance not required for function overloading | Inheritance is required for overriding |
Function overloading means defining multiple functions with the same name but different parameter lists in the same scope.
The parameter lists may differ by:
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
Calls:
add(2, 3); // Calls first function
add(2, 3, 4); // Calls second function
int area(int side)
{
return side * side;
}
double area(double radius)
{
return 3.14159 * radius * radius;
}
The compiler selects the function according to the argument type.
void show(int x, double y)
{
cout << "First";
}
void show(double x, int y)
{
cout << "Second";
}
Calls:
show(5, 2.5); // First
show(2.5, 5); // Second
Invalid:
int getValue()
{
return 10;
}
double getValue()
{
return 10.5;
}
The compiler cannot select a function using only the expected return type.
Functions cannot be overloaded only by changing the return type.
C++ allows many operators to be given special meaning for user-defined types.
Example idea:
Complex c3 = c1 + c2;
The + operator may be overloaded to add two complex-number objects.
Common examples include:
:: scope-resolution operator. member-access operator.* pointer-to-member operator?: conditional operatorsizeofFor Day 5, remember that operator overloading is a form of compile-time polymorphism.
Function overriding occurs when a derived class provides a new implementation of a base-class virtual function.
The derived function uses the same basic function signature.
class Animal
{
public:
virtual void sound()
{
cout << "Animal sound";
}
};
class Dog : public Animal
{
public:
void sound() override
{
cout << "Bark";
}
};
Here, Dog::sound() overrides Animal::sound().
| Basis | Overloading | Overriding |
|---|---|---|
| Meaning | Same name, different parameter list | Derived class replaces inherited virtual behaviour |
| Classes | Usually within the same scope or class | Requires base and derived classes |
| Parameters | Must differ | Must match the overriding signature |
| Binding | Compile time | Runtime when called virtually |
| Inheritance | Not required | Required |
| Return type | Cannot differ alone | Must follow C++ overriding return-type rules |
| Keyword | No special keyword required | virtual in base; override recommended in derived |
| Polymorphism | Compile-time | Runtime |
A virtual function is a base-class member function declared using the virtual keyword.
It allows a derived implementation to be selected at runtime when the function is called through a suitable base pointer or reference.
class Animal
{
public:
void sound()
{
cout << "Animal";
}
};
class Dog : public Animal
{
public:
void sound()
{
cout << "Dog";
}
};
Animal *p = new Dog;
p->sound();
Output:
Animal
The pointer type controls selection because the base function is not virtual.
class Animal
{
public:
virtual void sound()
{
cout << "Animal";
}
virtual ~Animal() = default;
};
class Dog : public Animal
{
public:
void sound() override
{
cout << "Dog";
}
};
Animal *p = new Dog;
p->sound();
delete p;
Output:
Dog
The actual object is a Dog, so Dog::sound() runs.
Base pointer p
│
v
Points to Dog object
│
v
sound() is virtual
│
v
Dog::sound() is selected
override SpecifierIn modern C++, write override in the derived class.
void sound() override
{
cout << "Dog";
}
Benefits:
If the base has:
virtual void show(int);
and the derived class accidentally writes:
void show(double) override;
the compiler reports an error because the signatures do not match.
A pure virtual function is a virtual function declared using = 0.
virtual return_type function_name(parameters) = 0;
class Shape
{
public:
virtual double area() const = 0;
virtual ~Shape() = default;
};
A derived class provides the implementation:
class Square : public Shape
{
private:
double side;
public:
Square(double s) : side(s)
{
}
double area() const override
{
return side * side;
}
};
A class containing at least one pure virtual function is an abstract class.
An abstract class is incomplete as a directly creatable type.
Invalid:
Shape s;
Valid:
Square square(5);
An abstract class:
A derived class must override all inherited pure virtual functions to become concrete. Otherwise, that derived class also remains abstract.
| Virtual Function | Pure Virtual Function |
|---|---|
Declared with virtual | Declared with virtual and = 0 |
| Usually has a usable base implementation | Makes the class abstract |
| Derived class may override it | Concrete derived class must implement it |
| Base class can normally be instantiated if no pure virtual function exists | Abstract base class cannot be directly instantiated |
Example: virtual void show() | Example: virtual void show() = 0 |
If objects may be deleted through a base-class pointer, the base class should normally have a virtual destructor.
class Base
{
public:
virtual ~Base()
{
}
};
This allows the derived destructor to run before the base destructor.
Base *p = new Derived;
delete p;
With a suitable virtual base destructor:
Derived destructor runs
Base destructor runs
This prevents incomplete destruction of the derived object.
Object slicing happens when a derived object is copied into a base object by value. The derived-specific part is lost.
Example:
Dog d;
Animal a = d;
Now a contains only the Animal part.
Runtime polymorphism is normally used through:
not by copying the derived object into a separate base object.
this Pointer and ExceptionsA constructor is a special member function that initializes an object.
void.class Student
{
private:
int roll;
public:
Student()
{
roll = 0;
}
};
When this object is created:
Student s;
the constructor runs automatically.
A default constructor is a constructor that can be called without arguments.
class Number
{
private:
int value;
public:
Number()
{
value = 0;
}
};
Call:
Number n;
A constructor with all parameters having default values can also act as a default constructor.
A parameterized constructor receives arguments.
class Number
{
private:
int value;
public:
Number(int v)
{
value = v;
}
int getValue() const
{
return value;
}
};
Creation:
Number n(25);
Now:
value = 25
A class can have several constructors with different parameter lists.
class Box
{
private:
int length;
int width;
public:
Box()
{
length = 0;
width = 0;
}
Box(int side)
{
length = side;
width = side;
}
Box(int l, int w)
{
length = l;
width = w;
}
};
Objects:
Box b1; // Default constructor
Box b2(5); // One-parameter constructor
Box b3(4, 6); // Two-parameter constructor
Constructor overloading is compile-time polymorphism.
A copy constructor creates a new object from an existing object of the same class.
ClassName(const ClassName &other);
class Number
{
private:
int value;
public:
Number(int v)
{
value = v;
}
Number(const Number &other)
{
value = other.value;
}
int getValue() const
{
return value;
}
};
Use:
Number n1(10);
Number n2 = n1;
Now n2 is initialized from n1.
If the parameter were passed by value, creating that parameter would itself require another copy-constructor call. A reference avoids that problem.
const?const allows copying from constant objects and prevents accidental modification of the source.
| Constructor Type | Purpose | Example Call |
|---|---|---|
| Default | Creates object without supplied arguments | Box b; |
| Parameterized | Initializes using arguments | Box b(5); |
| Copy | Creates object from another object | Box b2 = b1; |
DPC
A member initializer list initializes data members before the constructor body begins.
class Student
{
private:
int roll;
public:
Student(int r) : roll(r)
{
}
};
This is often better than assigning inside the constructor body.
It is required for some members, such as:
const data membersA destructor is a special member function that runs when an object is destroyed.
~ClassName()
{
// Cleanup
}
class Test
{
public:
Test()
{
cout << "Created ";
}
~Test()
{
cout << "Destroyed";
}
};
~.| Constructor | Destructor |
|---|---|
| Initializes an object | Cleans up an object |
| Same name as class | Same name with ~ |
| Can take parameters | Takes no parameters |
| Can be overloaded | Cannot be overloaded |
| Runs at object creation | Runs at object destruction |
| Cannot be virtual | Can be virtual |
Local objects in the same scope are usually destroyed in the reverse order of their completed construction.
If objects are created as:
A first;
A second;
A third;
Their destruction order is:
third
second
first
This follows a stack-like order.
A friend function is not a member of a class, but it is allowed to access that class’s private and protected members.
The function is declared using the friend keyword inside the class.
class Box
{
private:
int length;
public:
Box(int l) : length(l)
{
}
friend void showLength(const Box &b);
};
void showLength(const Box &b)
{
cout << b.length;
}
Call:
Box b(10);
showLength(b);
Output:
10
this pointer.If A is a friend of B, and B is a friend of C, A does not automatically become a friend of C.
If a function is a friend of a base class, it is not automatically a friend of the derived class.
Friend functions reduce strict data hiding. They should be used only when they provide a clear design benefit.
A complete class can also be declared as a friend.
class A
{
private:
int value;
friend class B;
};
Now the member functions of B can access permitted private and protected members of A.
this PointerInside a non-static member function, this is a pointer to the current object.
this → current object
If object s1 calls a member function, this points to s1.
If object s2 calls it, this points to s2.
thisclass Student
{
private:
int roll;
public:
void setRoll(int roll)
{
this->roll = roll;
}
};
Here:
this->roll means the object’s data member.roll means the function parameter.Without this, the statement:
roll = roll;
would assign the parameter to itself.
A member function can return the current object using *this.
class Number
{
private:
int value;
public:
Number &setValue(int value)
{
this->value = value;
return *this;
}
};
This can support method chaining:
n.setValue(10);
With multiple suitable functions, chaining may look like:
object.setA(10).setB(20);
thisthis is available in non-static member functions.this points to the object that called the function.this->member accesses a member of the current object.*this represents the current object.this pointer.this pointer because they are not members.An exception is an unusual condition or error that interrupts the normal flow of a program.
Examples:
Exception handling separates normal code from error-handling code.
C++ mainly uses:
trythrowcatchtry block
│
v
Error occurs?
/ \
No Yes
│ │
v v
Continue throw exception
│
v
matching catch
│
v
handle problem
try
{
int denominator = 0;
if (denominator == 0)
throw denominator;
cout << 10 / denominator;
}
catch (int value)
{
cout << "Division by zero";
}
Output:
Division by zero
try.throw reports an exception.catch handles a matching exception type.try
{
// Risky code
}
catch (int value)
{
cout << "Integer exception";
}
catch (double value)
{
cout << "Double exception";
}
catch (const char *message)
{
cout << message;
}
The matching catch block handles the exception.
catch (...)
{
cout << "Unknown exception";
}
catch (...) can catch exceptions that were not handled by earlier handlers.
It should normally be written after more specific catch blocks.
When an exception is thrown, control searches for a matching handler.
During this search, local objects in exited scopes are destroyed. This process is called stack unwinding.
This is one reason destructors are important in C++.
Inside a catch block, writing:
throw;
throws the current exception again so that another handler can process it.
C++ provides standard exception types through headers such as <exception> and <stdexcept>.
Common examples:
| Exception | General Meaning |
|---|---|
std::exception | Common base class for many standard exceptions |
std::runtime_error | Error detected during program execution |
std::logic_error | Problem related to program logic |
std::out_of_range | Value or index is outside an allowed range |
std::bad_alloc | Dynamic memory allocation failed |
throw std::runtime_error("Invalid operation");
A standard exception object often provides:
what()
which returns an explanatory message.
Java is a:
Java source files normally use the extension:
.java
Compiled Java bytecode files normally use:
.class
class Hello
{
public static void main(String[] args)
{
System.out.println("Hello");
}
}
Output:
Hello
| Part | Meaning |
|---|---|
class Hello | Defines a class named Hello |
public | Accessible to the Java launcher |
static | main can run without creating a Hello object |
void | main does not return a value |
String[] args | Stores command-line arguments |
System.out.println | Displays a line of output |
Suppose the source file is:
Hello.java
Compile:
javac Hello.java
The compiler creates:
Hello.class
Run:
java Hello
Java source code
Hello.java
│
│ javac compiler
v
Java bytecode
Hello.class
│
│ JVM
v
Native machine instructions
│
v
Program output
Bytecode is the intermediate instruction format produced by the Java compiler.
It is not normally the direct machine code of one particular processor.
The JVM reads or compiles bytecode for the current system.
The same .class file can often run on different systems if a compatible JVM is available.
This leads to the Java principle:
Write Once, Run Anywhere
| Type | Meaning |
|---|---|
| Source code | Human-readable Java program in a .java file |
| Bytecode | Intermediate JVM instructions in a .class file |
| Machine code | Processor-specific instructions executed by the hardware |
JVM stands for Java Virtual Machine.
The JVM is the runtime engine that executes Java bytecode.
JIT stands for Just-In-Time compiler.
A JIT compiler converts bytecode into native machine code while the program is running. This can improve performance.
JRE stands for Java Runtime Environment.
Conceptually, the JRE provides what is needed to run Java applications.
It includes:
JRE = JVM + Libraries needed to run Java programs
Modern JDK distributions may not provide a separate public JRE package in the old style. However, the JRE concept remains important for examinations.
JDK stands for Java Development Kit.
The JDK is used to develop, compile and run Java programs.
It includes:
javacjavaJDK = JRE concept + Development tools
and:
JRE = JVM + Runtime libraries
Therefore:
JDK
├── Development tools
│ ├── javac
│ ├── debugger tools
│ └── documentation tools
│
└── Runtime environment
├── JVM
└── Java libraries
| Basis | JDK | JRE | JVM |
|---|---|---|---|
| Full Form | Java Development Kit | Java Runtime Environment | Java Virtual Machine |
| Main Purpose | Develop and run Java programs | Run Java programs | Execute Java bytecode |
| Contains Compiler | Yes | No | No |
| Contains JVM | Yes | Yes | It is the JVM itself |
| Contains Libraries | Yes | Yes | Uses runtime support |
| Main Users | Developers | End users and runtime systems | Runtime engine |
| Key Tool | javac | Runtime libraries | Bytecode execution |
JDK is the largest
└── JRE or equivalent runtime components
└── JVM
Another memory sentence:
Develop with JDK, Run with JRE, Execute inside JVM.
Java uses automatic garbage collection.
Garbage collection finds objects that are no longer reachable and makes their memory available for reuse.
The programmer does not normally use delete as in C++.
| C++ | Java |
|---|---|
Can use destructors and delete for dynamically allocated objects | Uses automatic garbage collection |
| Supports deterministic destruction for local objects | Garbage-collection timing is generally not guaranteed |
Destructor syntax: ~ClassName() | No direct C++-style destructor |
class Student
{
int roll;
void display()
{
System.out.println(roll);
}
}
Student s = new Student();
Here:
Student is the class.s is a reference variable.new Student() creates the object.s.roll = 10;
s.display();
Java uses the extends keyword for class inheritance.
class Animal
{
void eat()
{
System.out.println("Eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("Barking");
}
}
A Java class can directly extend only one class.
Invalid idea:
class C extends A, B
Java avoids multiple inheritance of classes partly to prevent ambiguity such as the diamond problem involving class state and implementations.
An abstract class is declared using the abstract keyword.
It may contain:
abstract class Shape
{
abstract double area();
void display()
{
System.out.println("Shape");
}
}
A concrete subclass implements the abstract method:
class Square extends Shape
{
double side;
Square(double side)
{
this.side = side;
}
double area()
{
return side * side;
}
}
An interface defines a contract that implementing classes agree to follow.
It is declared using the interface keyword.
interface Printable
{
void print();
}
class Report implements Printable
{
public void print()
{
System.out.println("Printing report");
}
}
An interface method such as void print(); is public in this basic context. Therefore, the implementing method must be declared public; it cannot reduce access.
A Java interface can contain:
Interface fields are implicitly:
publicstaticfinalA class uses:
implements
to implement an interface.
A class can implement more than one interface.
interface Printable
{
void print();
}
interface Scannable
{
void scan();
}
class Machine implements Printable, Scannable
{
public void print()
{
System.out.println("Print");
}
public void scan()
{
System.out.println("Scan");
}
}
This gives Java a controlled form of multiple inheritance of type.
| Basis | Interface | Abstract Class |
|---|---|---|
| Declaration | interface | abstract class |
| Used By Class | implements | extends |
| Multiple Use | A class can implement multiple interfaces | A class can directly extend only one class |
| Constructors | Cannot have constructors | Can have constructors |
| Instance State | No ordinary instance fields | Can have ordinary instance fields |
| Methods | Can have abstract, default, static and permitted private methods | Can have abstract and concrete methods |
| Fields | Constants: public, static and final | Can have instance and static fields |
| Main Purpose | Define a contract or capability | Share state and partial implementation |
| Instantiation | Cannot be directly instantiated | Cannot be directly instantiated |
| Example Meaning | “Can do” relationship | Common base with “is-a” relationship |
Use an interface when unrelated classes need to follow the same contract.
Examples:
Use an abstract class when related classes need:
Java supports method overloading.
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
The compiler chooses the method according to the argument list.
Return type alone cannot create a valid overload in Java.
A subclass may provide a new implementation of an inherited instance method.
class Animal
{
void sound()
{
System.out.println("Animal sound");
}
}
class Dog extends Animal
{
@Override
void sound()
{
System.out.println("Bark");
}
}
The @Override annotation asks the compiler to check that the method truly overrides an inherited method.
An exception is an object representing an error or unusual condition.
Java exception handling uses:
trycatchfinallythrowthrowstry-catchtry
{
int result = 10 / 0;
System.out.println(result);
}
catch (ArithmeticException e)
{
System.out.println("Cannot divide by zero");
}
Output:
Cannot divide by zero
try begins.ArithmeticException.try block are skipped.catch block runs.finallyA finally block contains cleanup code.
It normally runs whether an exception occurs or not.
try
{
System.out.println("Try");
}
catch (Exception e)
{
System.out.println("Catch");
}
finally
{
System.out.println("Finally");
}
Output:
Try
Finally
No exception occurs, so catch does not run. finally runs.
try
{
int result = 10 / 0;
System.out.println(result);
}
catch (ArithmeticException e)
{
System.out.println("Catch");
}
finally
{
System.out.println("Finally");
}
Output:
Catch
Finally
finallyfinally normally runs, but there are exceptional situations where normal execution ends before it can run, such as forced JVM termination.
For exam questions, the usual rule is:
The finally block runs whether an exception occurs or not.
throw vs throwsthrowthrow explicitly throws one exception object.
throw new IllegalArgumentException("Invalid age");
throwsthrows is written in a method declaration to state that the method may pass certain exceptions to its caller.
void readFile() throws IOException
{
// File operations
}
throw | throws |
|---|---|
| Throws an exception object | Declares possible exception types |
| Used inside a method or block | Used in a method declaration |
| Followed by an exception object | Followed by one or more exception class names |
Example: throw new Exception() | Example: void f() throws Exception |
The compiler requires it to be handled or declared.
Example:
IOExceptionUsually represents a programming error and is not subject to the same compile-time handling requirement.
Examples:
ArithmeticExceptionNullPointerExceptionArrayIndexOutOfBoundsException| Checked | Unchecked |
|---|---|
| Checked at compile time for handling or declaration | Not required to be caught or declared |
| Often external or recoverable conditions | Often programming mistakes |
Example: IOException | Example: ArithmeticException |
| Feature | C++ | Java |
|---|---|---|
| Compilation | Commonly compiled to native code | Compiled to bytecode, then executed by JVM |
| Multiple Class Inheritance | Supported | Not supported |
| Multiple Interfaces | Not Java-style | Supported |
| Memory Management | Manual tools plus automatic object lifetime mechanisms | Automatic garbage collection |
| Destructor | Supported | No C++-style destructor |
| Friend Function | Supported | Not supported |
| Pointer Arithmetic | Supported | Not exposed like C++ pointer arithmetic |
| Operator Overloading | Supported for user-defined types | Generally not user-defined |
| Root Class | No universal required root class | Most classes ultimately derive from Object |
| Platform Model | Native binary is usually platform-specific | Bytecode runs through a compatible JVM |
| Pillar | Meaning | Main Tool |
|---|---|---|
| Encapsulation | Wrap data and functions together | Class |
| Abstraction | Show essentials and hide internal details | Abstract class, interface |
| Inheritance | Create a new class from an existing class | Base and derived classes |
| Polymorphism | One interface, many behaviours | Overloading and overriding |
Mnemonic:
EAIP = Every Animal Inherits Properties
Class = Blueprint
Object = Instance of class
Example:
Student = Class
s1 = Object
| Specifier | Same Class | Derived Class | Outside |
|---|---|---|---|
| private | Yes | No direct access | No |
| protected | Yes | Yes | No |
| public | Yes | Yes | Yes |
Encapsulation = Wrap data and functions
Data hiding = Restrict direct access
Abstraction = Show what; hide how
| Type | Pattern |
|---|---|
| Single | A → B |
| Multiple | A + B → C |
| Multilevel | A → B → C |
| Hierarchical | A → B and A → C |
| Hybrid | Combination of types |
Mnemonic:
SMMHH = Smart Minds Make Helpful Humans
One base class
↓ ↓
Two intermediate classes
↓ ↓
One final class
Problem:
Duplicate base-class subobjects and ambiguity
Solution:
Virtual inheritance
Example:
class B : virtual public A
class C : virtual public A
| Overloading | Overriding |
|---|---|
| Same name, different parameter list | Derived class changes inherited virtual behaviour |
| Compile time | Runtime when called virtually |
| Inheritance not required | Inheritance required |
| Return type alone is insufficient | Signature must satisfy overriding rules |
Virtual function:
virtual void show();
Pure virtual function:
virtual void show() = 0;
A class with a pure virtual function is abstract.
An abstract class cannot be directly instantiated.
| Type | Purpose |
|---|---|
| Default | Called without arguments |
| Parameterized | Receives initial values |
| Copy | Initializes from an existing object |
Rules:
~ClassName();
Rules:
this pointerthis Pointerthis → current object
this->member → current object's member
*this → current object
Not available in:
try
{
if (error)
throw value;
}
catch (Type value)
{
// Handle error
}
Keywords:
try, throw, catch
Catch all:
catch (...)
Source code (.java)
│
│ javac
v
Bytecode (.class)
│
│ JVM
v
Native machine execution
JDK = Development tools + Runtime environment
JRE = JVM + Runtime libraries
JVM = Executes bytecode
Mnemonic:
JDK = Develop
JRE = Run
JVM = Execute bytecode
| Interface | Abstract Class |
|---|---|
| Contract | Partial base class |
implements | extends |
| Multiple interfaces allowed | Only one direct class parent |
| No constructors | Can have constructors |
| No ordinary instance fields | Can have instance fields |
| Constants and interface methods | Abstract and concrete methods |
try → risky code
catch → handles matching exception
finally → normally runs for cleanup
throw → throws an object
throws → declares possible exception types
Which OOP concept combines data and related functions inside one unit?
A. Inheritance
B. Encapsulation
C. Compilation
D. Interpretation
An object is best described as:
A. A compiler
B. A class declaration keyword
C. An instance of a class
D. A header file
Which OOP concept shows essential features while hiding unnecessary implementation details?
A. Abstraction
B. Inheritance
C. Recursion
D. Overloading
Which access specifier permits access inside the class and its derived classes but normally not through outside objects?
A. public
B. protected
C. private only
D. friend
Which list contains all four main OOP pillars?
A. Class, Object, Function and Pointer
B. Encapsulation, Abstraction, Inheritance and Polymorphism
C. Compilation, Linking, Loading and Execution
D. Array, Stack, Queue and Tree
When one derived class inherits from one base class, it is called:
A. Multiple inheritance
B. Multilevel inheritance
C. Single inheritance
D. Hybrid inheritance
Which inheritance structure represents multilevel inheritance?
A. A → B → C
B. A + B → C
C. A → B and A → C
D. A only
When Circle, Square and Triangle all inherit from Shape, the inheritance type is:
A. Multiple
B. Hierarchical
C. Multilevel
D. Single only
In C++, the diamond problem is commonly solved using:
A. Function overloading
B. Virtual inheritance
C. A friend function
D. A destructor
What is the construction order when a Derived object is created?
A. Derived constructor, then base constructor
B. Base constructor, then derived constructor
C. Only the base constructor
D. Only the derived constructor
Function overloading is normally an example of:
A. Runtime polymorphism
B. Compile-time polymorphism
C. Data hiding
D. Garbage collection
Which change alone cannot create a valid function overload?
A. Changing the number of parameters
B. Changing parameter types
C. Changing the order of parameter types
D. Changing only the return type
Which combination is required for typical runtime polymorphism in C++?
A. Macro and structure
B. Inheritance, overriding and virtual function call
C. File and pointer only
D. Constructor overloading only
What does the following declaration represent?
virtual void display() = 0;
A. Constructor
B. Friend function
C. Pure virtual function
D. Static function
A class containing at least one pure virtual function is called:
A. Friend class
B. Abstract class
C. Final object
D. Static class
Which statement about a C++ constructor is correct?
A. It must return int.
B. It has the same name as the class.
C. It begins with ~.
D. It can always be virtual.
Which constructor creates a new object from an existing object of the same class?
A. Default constructor
B. Parameterized constructor
C. Copy constructor
D. Destructor
Which statement about a friend function is correct?
A. It must be a member of the class.
B. It cannot access private data.
C. It is not a member but can be granted private access.
D. It automatically becomes a friend of every derived class.
Inside a non-static C++ member function, this points to:
A. The base class only
B. The current object
C. The compiler
D. The next object in memory
Which sequence correctly represents basic C++ exception handling?
A. catch → try → throw
B. throw → try → catch only
C. try → throw → matching catch
D. finally → throw → try
Which component contains development tools such as the Java compiler?
A. JVM
B. JRE
C. JDK
D. Bytecode
Which component directly executes Java bytecode?
A. JDK documentation tool
B. JVM
C. Java source file
D. Text editor
The Java compiler normally converts a .java file into:
A. A .class bytecode file
B. A .cpp file
C. A database table
D. An HTML document
Which statement correctly compares a Java interface and an abstract class?
A. An interface can have constructors, but an abstract class cannot.
B. A class can implement multiple interfaces but directly extend only one class.
C. An abstract class cannot contain implemented methods.
D. Interface fields are ordinary private instance variables.
What is the output?
public class Test
{
public static void main(String[] args)
{
try
{
int x = 10 / 0;
System.out.println("Try");
}
catch (ArithmeticException e)
{
System.out.println("Catch");
}
finally
{
System.out.println("Finally");
}
}
}
A. Try
B. Catch
C. Catch followed by Finally
D. Try followed by Finally
| Q. | Answer | Q. | Answer | Q. | Answer | Q. | Answer | Q. | Answer |
|---|---|---|---|---|---|---|---|---|---|
| 1 | B | 6 | C | 11 | B | 16 | B | 21 | C |
| 2 | C | 7 | A | 12 | D | 17 | C | 22 | B |
| 3 | A | 8 | B | 13 | B | 18 | C | 23 | A |
| 4 | B | 9 | B | 14 | C | 19 | B | 24 | B |
| 5 | B | 10 | B | 15 | B | 20 | C | 25 | C |
Encapsulation wraps data and the functions working on that data inside one unit, usually a class.
A class is a blueprint. An object is an actual instance created from that blueprint.
Class: Student
Object: s1
Abstraction shows essential operations and hides unnecessary internal details.
Example:
User calls car.start()
The user does not need to know every internal engine step.
A protected member is accessible:
It is not normally accessible directly through an outside object.
The four pillars are:
Encapsulation
Abstraction
Inheritance
Polymorphism
Mnemonic:
EAIP
Single inheritance has:
One base class → One derived class
Example:
Animal → Dog
Multilevel inheritance forms a chain.
Animal → Mammal → Dog
One base class has multiple derived classes:
Shape
/ | \
Circle Square Triangle
The diamond problem can create two copies of the same base-class subobject.
Virtual inheritance provides one shared virtual base subobject.
class B : virtual public A
class C : virtual public A
Construction order:
Base constructor
Derived constructor
Destruction happens in reverse:
Derived destructor
Base destructor
The compiler selects an overloaded function according to its parameter list.
Example:
add(int, int)
add(double, double)
This is invalid:
int getValue();
double getValue();
The calls have identical argument lists, so the compiler cannot choose using only the return type.
Typical C++ runtime polymorphism requires:
The = 0 syntax makes it pure virtual:
virtual void display() = 0;
It helps define an abstract interface.
A class with at least one pure virtual function is abstract.
An abstract class cannot be directly instantiated.
Invalid:
AbstractClass object;
Constructor rules:
A copy constructor initializes a new object from an existing object.
Common form:
ClassName(const ClassName &other);
A friend function:
this pointerFriendship is not automatically inherited or transitive.
Inside a non-static member function:
this → address of the current object
this->member → member of the current object
*this → current object
Example:
void setValue(int value)
{
this->value = value;
}
Here, this->value is the data member, while value is the parameter.
try → throw → matching catchThe normal C++ exception flow is:
try block
│
v
Error detected
│
v
throw exception
│
v
Matching catch block
│
v
Exception handled
Example:
try
{
throw 10;
}
catch (int value)
{
cout << value;
}
Output:
10
JDK stands for Java Development Kit.
It contains:
The javac compiler is part of the JDK.
Memory rule:
JDK = Develop
JRE = Run
JVM = Execute bytecode
JVM stands for Java Virtual Machine.
It executes Java bytecode.
Program flow:
Java source file
│
│ javac
v
Java bytecode
│
│ JVM
v
Machine execution
.class bytecode fileThe Java compiler normally changes:
Hello.java
into:
Hello.class
The .class file contains Java bytecode.
The JVM executes this bytecode.
A Java class can:
Example:
class Machine implements Printable, Scannable
{
// Implement required methods
}
An interface cannot have a constructor. An abstract class can have constructors and ordinary instance fields.
The statement:
int x = 10 / 0;
causes an ArithmeticException.
Therefore:
"Try" is not printed because execution leaves the try block at the error.catch block prints "Catch".finally block prints "Finally".Output:
Catch
Finally
Complete these tests without looking at the main notes.
Fill in the missing terms:
Mnemonic:
EAIP = Every Animal Inherits Properties
Consider:
class Car
{
public:
void start()
{
cout << "Started";
}
};
Car c1;
Car c2;
Identify:
Class = Car
Objects = c1 and c2
Member function = start()
Remember:
Class = Blueprint
Object = Instance
Match each description:
| Description | Concept |
|---|---|
| Place data and related functions in one class | ? |
| Make a data member private | ? |
Provide a simple start() function while hiding internal engine steps | ? |
| Description | Concept |
|---|---|
| Place data and related functions in one class | Encapsulation |
| Make a data member private | Data hiding |
Hide engine steps behind start() | Abstraction |
Memory rule:
Encapsulation = Wrap
Data hiding = Restrict
Abstraction = Hide complexity
Animal
│
v
Dog
Answer:
Single inheritance
Writer Speaker
\ /
\ /
Person
Answer:
Multiple inheritance
Animal
│
v
Mammal
│
v
Dog
Answer:
Multilevel inheritance
Shape
/ | \
Circle Square Triangle
Answer:
Hierarchical inheritance
Person
/ \
Student Employee
\ /
Assistant
Answer:
Hybrid inheritance
Study the diagram:
Person
/ \
Student Employee
\ /
Assistant
Assistant receive two copies of Person?Student and Employee both inherit from Person, and Assistant inherits from both.Example:
class Student : virtual public Person
{
};
class Employee : virtual public Person
{
};
Result:
Assistant has one shared Person base subobject.
int add(int a, int b);
double add(double a, double b);
Answer:
Function overloading
Reason:
Same function name, different parameter types
Binding:
Compile time
class Animal
{
public:
virtual void sound()
{
cout << "Animal";
}
};
class Dog : public Animal
{
public:
void sound() override
{
cout << "Bark";
}
};
Answer:
Function overriding
Reason:
Derived class provides a new implementation of a base virtual function
Binding through a base pointer or reference:
Runtime
Predict the output:
class Base
{
public:
virtual void show()
{
cout << "Base";
}
virtual ~Base() = default;
};
class Derived : public Base
{
public:
void show() override
{
cout << "Derived";
}
};
int main()
{
Base *p = new Derived;
p->show();
delete p;
}
p is a base-class pointer.Derived object.show() is virtual.Output:
Derived
Consider:
class Shape
{
public:
virtual double area() const = 0;
};
area()?Shape?Shape be directly instantiated?Invalid:
Shape s;
A concrete derived class must implement area().
Predict the output:
class Test
{
public:
Test()
{
cout << "Default ";
}
Test(int value)
{
cout << "Parameterized ";
}
};
int main()
{
Test a;
Test b(10);
}
Test a;
calls the default constructor.
Test b(10);
calls the parameterized constructor.
Output:
Default Parameterized
Predict the order:
class Base
{
public:
Base()
{
cout << "Base constructor ";
}
~Base()
{
cout << "Base destructor ";
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived constructor ";
}
~Derived()
{
cout << "Derived destructor ";
}
};
int main()
{
Derived object;
}
Construction:
Base constructor
Derived constructor
Destruction:
Derived destructor
Base destructor
Complete output:
Base constructor Derived constructor Derived destructor Base destructor
Memory rule:
Construct: Parent → Child
Destroy: Child → Parent
Consider:
class Number
{
private:
int value;
public:
Number(int value) : value(value)
{
}
friend void display(const Number &n);
};
void display(const Number &n)
{
cout << n.value;
}
int main()
{
Number n(50);
display(n);
}
display() is not a member function, but Number declares it as a friend.
It can access the private member value.
Output:
50
this PointerPredict the value:
class Number
{
private:
int value;
public:
void setValue(int value)
{
this->value = value;
}
int getValue() const
{
return value;
}
};
int main()
{
Number n;
n.setValue(25);
cout << n.getValue();
}
Inside setValue():
this->value
means the object’s data member.
The right-side:
value
means the parameter.
The data member receives 25.
Output:
25
Predict the output:
try
{
cout << "A ";
throw 5;
cout << "B ";
}
catch (int value)
{
cout << value;
}
"A " is printed.throw 5 transfers control to the matching catch.throw in the try block is skipped.Output:
A 5
"B " is not printed.
Complete the diagram:
__________
├── Development tools such as javac
└── __________
├── Runtime libraries
└── __________ executes bytecode
JDK
├── Development tools such as javac
└── JRE
├── Runtime libraries
└── JVM executes bytecode
Exam formula:
JDK = JRE + Development tools
JRE = JVM + Runtime libraries
Complete the flow:
Hello.____
│
│ Java compiler: ______
v
Hello.____
│
│ ______
v
Execution
Hello.java
│
│ Java compiler: javac
v
Hello.class
│
│ JVM
v
Execution
Choose the correct feature.
Can have constructors:
Abstract class
A class can use more than one of these:
Interface
Can contain ordinary instance state:
Abstract class
Mainly defines a capability or contract:
Interface
Can contain both abstract and concrete methods:
Abstract class
Modern Java interfaces can also contain default and static methods, but their main purpose remains defining a contract.
Predict the output:
try
{
System.out.println("Start");
int x = 20 / 5;
System.out.println(x);
}
catch (ArithmeticException e)
{
System.out.println("Error");
}
finally
{
System.out.println("End");
}
The calculation is valid:
20 / 5 = 4
No exception occurs, so the catch block does not run.
The finally block runs.
Output:
Start
4
End
Answer these aloud without reading the main notes.
override specifier do?this pointer?this pointer represent?this available in a static member function?catch (...) mean?javac?try?catch?finally?throw and throws?Use your score from the 25 practice questions.
| Correct Answers | Level | Required Action |
|---|---|---|
| 23–25 | Excellent | Revise the quick sheet tomorrow |
| 20–22 | Good | Review incorrect answers once |
| 16–19 | Developing | Repeat inheritance and polymorphism |
| 10–15 | Weak | Restudy all five hourly sections |
| 0–9 | Beginning | Read the material again from Section 1 |
| Questions Missed | Topic to Revise |
|---|---|
| 1–5 | OOP pillars, class, object and access specifiers |
| 6–10 | Inheritance types and diamond problem |
| 11–15 | Polymorphism, overloading, overriding and virtual functions |
| 16–20 | Constructors, friends, this and exceptions |
| 21–25 | JDK, JRE, JVM, bytecode, interfaces and Java exceptions |
Study:
Write from memory:
Class = Blueprint
Object = Instance
Encapsulation = Wrap
Data hiding = Restrict
Abstraction = Hide complexity
Practice:
BankAccount class.Study:
Draw these diagrams from memory:
Single:
A → B
Multiple:
A + B → C
Multilevel:
A → B → C
Hierarchical:
A → B
A → C
Hybrid:
Combination of types
Memorize:
Construction: Base → Derived
Destruction: Derived → Base
Practice:
virtual public in the correct place.Study:
Memorize:
Overloading:
Same name + different parameters
Overriding:
Derived class + inherited virtual function
Pure virtual:
virtual void show() = 0;
Practice:
add() functions.Animal class with virtual sound().Dog override.Study:
this pointerWrite from memory:
Default constructor:
ClassName();
Parameterized constructor:
ClassName(int value);
Copy constructor:
ClassName(const ClassName &other);
Destructor:
~ClassName();
Practice:
this->value = value.try-throw-catch example.Study:
try-catch-finallythrow and throwsDraw this flow:
Source (.java)
│
│ javac
v
Bytecode (.class)
│
│ JVM
v
Execution
Memorize:
JDK = Develop
JRE = Run
JVM = Execute bytecode
Practice:
finally runs.Before answering an OOP question, remember:
Four pillars:
Encapsulation, Abstraction, Inheritance, Polymorphism
Five inheritance types:
Single, Multiple, Multilevel, Hierarchical, Hybrid
Diamond solution:
Virtual inheritance
Overloading:
Compile time, different parameters
Overriding:
Runtime, base and derived classes
Pure virtual:
= 0
Constructor:
Same name, no return type
Destructor:
~ClassName, cannot be overloaded
Friend:
Non-member with granted access
this:
Current object
C++ exceptions:
try, throw, catch
Java:
JDK develops
JRE runs
JVM executes bytecode
Java interface:
Contract, multiple interfaces allowed
Java abstract class:
Partial base class, constructors and instance state allowed
Java exceptions:
try, catch, finally