What is ERP-1
What is ERP-1
Early Binding, also known as Static Binding, refers to the process of linking a method call
or variable reference to its definition at compile-time. In early binding, the method or
variable is resolved and assigned by the compiler, meaning that the method call or reference
to a variable is resolved before the program starts executing.
Key Points:
class MyClass {
Late Binding, also known as Dynamic Binding, refers to the process of linking a method
call to its definition at runtime. This occurs when the method to be called is determined
during the execution of the program, typically used in the context of method overriding with
inheritance or polymorphism.
Key Points:
Occurs at Runtime: The method or variable is resolved during the execution of the
program, and the actual method is determined based on the object's type.
Typically Associated with Instance Methods: Methods that are overridden in
subclasses, where the actual method to be invoked is determined at runtime.
Slower: Late binding is slower than early binding because it involves additional
overhead at runtime to determine which method or variable to call.
class Animal {
void sound() {
void sound() {
System.out.println("Dog barks");
myAnimal.sound(); // Late binding: The method resolved at runtime based on the object
type
In this example, myAnimal.sound() calls the sound method of the Dog class, even though the
reference is of type Animal. This decision happens at runtime, hence it is an example of late
binding.
Differences Between Early Binding and Late Binding:
In summary:
Early Binding happens at compile-time and is used for static methods and variables,
where the method call is resolved by the compiler.
Late Binding happens at runtime and is used for instance methods (especially
overridden ones), where the method call is resolved based on the actual object type at
execution time.
1. Encapsulation:
Definition: Encapsulation is the concept of bundling the data (variables) and the
methods (functions) that operate on the data into a single unit called a class. It also
restricts direct access to some of an object's components and can prevent the
accidental modification of data.
Key Points:
o Data is hidden inside the class and can only be accessed or modified using
getter and setter methods.
o Helps achieve data hiding and abstraction.
Example:
class Person {
return name;
this.name = name;
2. Inheritance:
Example:
class Animal {
void speak() {
System.out.println("Animal speaks");
void speak() {
System.out.println("Dog barks");
}
3. Polymorphism:
Example:
class Animal {
void speak() {
System.out.println("Animal speaks");
@Override
void speak() {
System.out.println("Dog barks");
class Main {
}
}
4. Abstraction:
Definition: Abstraction is the process of hiding the complexity and only showing the
essential features of an object. It can be achieved through abstract classes and
interfaces in OOP.
Key Points:
o Allows focusing on what an object does, rather than how it does it.
o Abstract classes can have abstract methods (methods without implementation),
which must be implemented by subclasses.
Example:
void sound() {
System.out.println("Barks");
Example:
class Car {
String model;
int year;
void drive() {
class Main {
myCar.model = "Toyota";
myCar.year = 2020;
myCar.drive();
6. Message Passing:
Example:
class Printer {
System.out.println(message);
class Main {
public static void main(String[] args) {
1. Encapsulation - Hiding internal state and requiring all interaction to be performed via
methods.
2. Inheritance - Creating new classes based on existing ones to promote code reuse.
3. Polymorphism - Allowing methods to behave differently based on the object type.
4. Abstraction - Hiding complexity by exposing only necessary features.
5. Classes and Objects - A class is a blueprint, and objects are instances created from
the class.
6. Message Passing - Objects communicate with each other by invoking methods and
exchanging information.
In simple terms, message passing is how objects interact with one another in an OOP system.
The idea is that objects don’t directly access each other’s data or methods, but instead, they
send messages to each other (usually by invoking methods) to request actions or share
information.
Key Points:
1. Method Invocation: When one object calls a method of another object, it's essentially
passing a message to the other object to perform some action.
2. Passing Data: The message can include parameters or data that the receiving object
needs to process the request.
3. Encapsulation: The concept supports the idea of encapsulation because objects do
not directly manipulate each other’s internal state. Instead, they communicate via
methods (messages).
Example:
Let's take an example where two objects, Car and Driver, communicate using message
passing.
class Car {
void startEngine() {
class Driver {
Car car;
Driver(Car car) {
this.car = car;
void driveCar() {
Driver driver = new Driver(myCar); // Create a Driver object and pass the Car object
driver.driveCar(); // Driver sends a message to Car to start the engine and drive
}
In the example:
Message Passing: The Driver object sends a message to the Car object by calling the
startEngine() method.
Encapsulation: The Driver doesn't directly start the engine or access internal
properties of the Car object. Instead, it sends a message (method call) to the Car to
request the engine start.
Summary:
Message passing in OOP is a way of interacting with objects through methods, where one
object invokes a method on another object, sending a message to perform an action. It is an
essential concept that promotes modularity, data encapsulation, and abstraction in object-
oriented systems.
Common Scenario:
In many cases, a qualified association can represent situations where a container class holds
references to multiple objects, but each reference can be uniquely accessed via a qualification
value. For example, a Library class could have multiple Book objects, and a qualified
association might use a ISBN number as a qualification to uniquely identify each book.
Let's consider an example of a Library and a Book class. The Library holds multiple Book
objects, but each Book can be accessed using its ISBN number as a qualification.
+----------------+ 1 +----------------+
+----------------+ +----------------+
+----------------+
The Library class has a qualified association with the Book class.
The qualification is based on the ISBN of each book.
The Library can access each Book uniquely by its ISBN.
class Book {
String isbn;
String title;
this.isbn = isbn;
this.title = title;
class Library {
books.put(book.isbn, book);
}
Explanation:
The Library holds a collection of Book objects in a Map, where the key is the ISBN
(the qualification) and the value is the Book object.
The getBookByISBN method allows accessing any Book in the Library using the
ISBN as a qualification.
Summary:
A qualified association is a relationship between two classes where one class holds a
collection of objects from another class, and a qualification (like a key or identifier) is used to
access specific objects within that collection. This provides a more structured way to model
relationships that require identification or filtering within a group of related objects.
Abstract Class:
Example:
void eat() {
void sound() {
Interface:
An interface in Java is a reference type, similar to a class, that can contain only abstract
methods, default methods, and static methods. Interfaces define a contract that a class must
adhere to if it implements the interface. A class that implements an interface must provide
implementations for all abstract methods of the interface.
Key Features of Interface:
1. Abstract Methods: All methods in an interface are abstract by default (until Java 8,
after which default and static methods can be added).
2. Default Methods: Since Java 8, interfaces can have methods with default
implementations using the default keyword.
3. Static Methods: Interfaces can contain static methods.
4. No Instance Variables: Interfaces cannot have instance variables; only constants
(static and final variables) are allowed.
5. Multiple Inheritance: A class can implement multiple interfaces, enabling multiple
inheritance.
6. Cannot Have Constructors: Interfaces do not have constructors.
Example:
interface Animal {
void sound();
// Default method
Abstract Class: When you want to provide a common base class with some shared
implementation, and the classes you are working with are closely related in a
hierarchy.
Interface: When you want to define a contract for behavior that can be implemented
by any class, irrespective of its position in the class hierarchy. Interfaces are often
used for cross-cutting concerns like logging, event handling, or defining APIs.
Summary:
Abstract Class: Can have both abstract and concrete methods, instance variables, and
constructors. It allows for a more hierarchical design.
Interface: Primarily defines a contract with abstract methods and constants, and is
more suited for defining capabilities or behaviors that can be implemented across
different class hierarchies.
Parameter Passing:
Parameter passing refers to how arguments (or values) are passed to a method or function
when it is invoked. In programming, there are mainly two ways to pass arguments to
functions or methods: Call-by-Value and Call-by-Reference.
1. Call-by-Value:
o Definition: In Call-by-Value, a copy of the actual value is passed to the
function. This means any changes made to the parameter inside the function
do not affect the original argument.
o Effect on Original Value: Since the function operates on a copy, the original
value in the calling environment is unchanged after the function call.
2. Call-by-Reference:
o Definition: In Call-by-Reference, a reference (memory address) of the
argument is passed to the function, so the function works with the actual data.
This means that changes made to the parameter inside the function directly
affect the original argument.
o Effect on Original Value: Since the function works directly with the
argument, changes to the parameter inside the function also change the
original value.
Call-by-Value Example:
class CallByValueExample {
int x = 10;
modifyValue(x);
Output:
Explanation:
class CallByReferenceExample {
modifyValue(arr);
Output:
Explanation:
Summary:
Call-by-Value: A copy of the value is passed to the function, and changes made
inside the function do not affect the original value.
Call-by-Reference: The reference (or memory address) of the original argument is
passed to the function, and changes made inside the function directly affect the
original value.
1. THROW:
o The throw keyword is used to explicitly throw an exception from a method or
block of code.
o When you use throw, you are creating and throwing a specific exception.
o throw is used inside the method to indicate that something went wrong, and
the method should terminate or pass the exception to a higher level in the call
stack.
o Example:
o class ThrowExample {
o public static void checkAge(int age) {
o if (age < 18) {
o throw new ArithmeticException("Age must be 18 or above.");
o } else {
o System.out.println("Age is valid.");
o }
o }
o
o public static void main(String[] args) {
o checkAge(15); // This will throw an exception
o }
}o
2. THROWS:
o The throws keyword is used in the method signature to declare that the
method might throw one or more exceptions.
o throws tells the calling code that it should handle or declare the exceptions that
could arise from the method.
o This is used for checked exceptions (exceptions that are checked at compile-
time).
o Example:
o class ThrowsExample {
o public static void readFile() throws IOException {
o FileReader fr = new FileReader("file.txt");
o BufferedReader br = new BufferedReader(fr);
o br.readLine();
o }
o
o public static void main(String[] args) {
o try {
o readFile();
o } catch (IOException e) {
o System.out.println("IOException caught: " + e);
o }
o }
o }
1. FINAL:
o final is a keyword used to declare constants, prevent method overriding, and
prevent inheritance of classes.
o It can be applied to:
Variables: When a variable is declared as final, its value cannot be
changed after initialization. These are constants.
Methods: When a method is declared as final, it cannot be overridden
by any subclass.
Classes: When a class is declared as final, it cannot be subclassed
(inherited).
o Example of final variable:
o final int MAX_VALUE = 100;
o // MAX_VALUE = 200; // This will cause an error as the variable is final.
o Example of final method:
o class Parent {
o public final void display() {
o System.out.println("Display in Parent");
o }
o }
o
o class Child extends Parent {
o // public void display() { } // This will cause an error, as the method is final.
o }
o Example of final class:
final class FinalClass {
o
o // This class cannot be subclassed.
}
o
2. FINALLY:
o finally is a block of code that is always executed after the try block, whether
an exception is thrown or not. It is used to release resources or perform
cleanup actions like closing files or database connections.
o The finally block is optional, but when it is present, it will always execute
after the try block (even if there is a return statement in the try block or if an
exception occurs).
o Example of finally block:
o class FinallyExample {
o public static void main(String[] args) {
o try {
o System.out.println("Inside try block.");
o int result = 10 / 0; // This will throw an exception
o } catch (ArithmeticException e) {
o System.out.println("Inside catch block.");
o } finally {
o System.out.println("Inside finally block.");
o }
o }
o }
Output:
Summary:
throw is used to throw an exception explicitly in the code, while throws is used in
the method signature to declare the exceptions that might be thrown.
final is used to declare constants, prevent method overriding, or prevent class
inheritance, while finally is used to ensure that a block of code is executed after the
try-catch blocks, regardless of whether an exception occurs.
Exceptions:
An exception in Java is an event that disrupts the normal flow of the program's execution.
Exceptions can occur for various reasons, such as invalid input, file not found, or dividing by
zero. When an exception occurs, it creates an object that contains information about the error
and its cause, and the program may either handle the exception or terminate.
Java has a built-in mechanism for handling exceptions using try, catch, throw, throws, and
finally blocks.
User-Defined Exceptions:
Example:
// User-defined exception
}
class TestUserDefinedException {
} else {
System.out.println("Valid age");
try {
} catch (InvalidAgeException e) {
System-Defined Exceptions:
System-defined exceptions are exceptions that are already provided by Java in the
standard library. These exceptions are subclasses of the Throwable class and are
divided into:
o Checked exceptions: These exceptions must be either caught or declared to
be thrown (e.g., IOException, SQLException).
o Unchecked exceptions: These exceptions do not need to be explicitly handled
or declared. They are subclasses of RuntimeException (e.g.,
NullPointerException, ArrayIndexOutOfBoundsException).
class TestSystemDefinedException {
public static void main(String[] args) {
try {
} catch (ArrayIndexOutOfBoundsException e) {
this Keyword:
The this keyword in Java refers to the current instance of the class. It is used within
an instance method or constructor to refer to the current object.
o It can be used to differentiate between class attributes and parameters if they
have the same name.
o It can also be used to call another constructor within the same class
(constructor chaining).
Example:
class Person {
Person(String name) {
void display() {
}
public class TestThis {
person.display();
super Keyword:
The super keyword refers to the parent class of the current object. It is used to:
o Access the parent class's methods and constructors.
o Call a parent class's constructor from a subclass.
o Invoke the parent class method when it is overridden in the subclass.
Example:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
}
public class TestSuper {
dog.sound(); // Will call the method from Dog class and use the parent method
1. == Operator:
o The == operator in Java is used to compare references (memory locations) of
objects, not the content. For String objects, == checks if both string variables
point to the same object in memory (i.e., they refer to the same memory
location).
Example:
2. equals() Method:
o The equals() method is used to compare the content of the strings. It checks if
the actual characters inside the two String objects are the same, even if they
are stored in different memory locations.
Example:
Summary:
Exceptions are events that disrupt the normal flow of execution in a program. They
can be user-defined (created by the programmer) or system-defined (provided by the
Java library).
The this keyword refers to the current instance of the class, while super refers to the
parent class of the current object.
The == operator compares object references, while the equals() method compares the
contents of the objects.
Example:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
}
2. Encapsulation:
Encapsulation is an OOP concept where the internal details of an object are hidden from the
outside world. Only the necessary functionality is exposed, usually through getter and setter
methods. This helps in maintaining data integrity and protecting the internal state of the
object.
Example:
class Person {
return name;
this.name = name;
}
3. Link and Association:
Association refers to a relationship between two classes where one class can be
associated with another, representing a "has-a" relationship. It can be one-to-one, one-
to-many, or many-to-many.
Link refers to the connection or reference between objects of different classes. It can
be unidirectional or bidirectional.
4. Covariant Return:
Covariant Return is a feature in Java where a method in a subclass can return a type that is a
subclass of the return type declared in the superclass method. This allows more specific
return types while still adhering to the parent method's signature.
Example:
class Animal {
Animal getInstance() {
@Override
Dog getInstance() {
5. Polymorphism:
Polymorphism is the ability of an object to take many forms. In Java, this is implemented
through method overloading (compile-time polymorphism) and method overriding (runtime
polymorphism). It allows a single method or object to behave differently based on the
context.
class Calculator {
int add(int a, int b) {
return a + b;
return a + b;
6. Abstract Class:
An abstract class is a class that cannot be instantiated on its own and may contain abstract
methods (methods without a body) that must be implemented by its subclasses. It provides a
blueprint for other classes.
Example:
void sleep() {
System.out.println("Animal is sleeping");
@Override
void sound() {
System.out.println("Dog barks");
}
}
7. Dynamic Binding:
Dynamic Binding refers to the process where the method that gets called is determined at
runtime, not compile-time. This is typically used in the context of method overriding in
polymorphism.
Example:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
8. Method Overriding:
Method Overriding allows a subclass to provide a specific implementation of a method that is
already defined in its superclass. The overridden method must have the same name, return
type, and parameters.
Example:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
9. Properties of OOP:
Encapsulation: Bundling of data and methods that operate on the data into a single
unit (class).
Abstraction: Hiding the implementation details and showing only the essential
features of the object.
Inheritance: Mechanism that allows one class to inherit the properties and behaviors
of another class.
Polymorphism: The ability of an object to take many forms, achieved through
method overloading and overriding.
Java does not support multiple inheritance directly through classes. This means that a class
cannot inherit from more than one class, as multiple inheritance can lead to ambiguity (e.g.,
the Diamond Problem). However, Java provides a way to implement multiple inheritance
using interfaces.
Java implements multiple inheritance using interfaces. In Java, a class can implement
multiple interfaces, thereby achieving multiple inheritance of behavior without the issues
related to multiple inheritance from classes.
Example:
interface A {
void methodA();
interface B {
void methodB();
}
class C implements A, B {
@Override
@Override
obj.methodA();
obj.methodB();
In this example, class C implements both interfaces A and B, thereby achieving the effect of
multiple inheritance.
2. Method Overloading:
Method overloading refers to defining multiple methods in the same class with the same
name but with different parameter lists (i.e., different numbers of parameters, types, or both).
Example:
class Calculator {
return a + b;
return a + b + c;
In this example, the method add is overloaded with two different parameter lists: one with
two integers and the other with three integers.
Method Overloading:
o Occurs when two or more methods in the same class have the same name but
different parameters (different number or types of parameters).
o It is resolved at compile-time (also known as compile-time polymorphism).
o It doesn't change the behavior of the method; it's just a way to provide
multiple methods with the same name but different parameters.
class Demo {
void display(int a) {
Method Overriding:
o Method overriding occurs when a subclass provides a specific implementation
for a method that is already defined in its superclass.
o It is resolved at runtime (also known as runtime polymorphism).
o The method signature (name and parameters) in the subclass must be the same
as that in the superclass.
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
Inheritance in Java:
1. Single Inheritance:
Single inheritance occurs when a class (child class) inherits from one and only one class
(parent class).
Example:
class Animal {
void sound() {
void sound() {
System.out.println("Dog barks");
}
In this example, Dog inherits from Animal and can access or override the sound() method.
2. Multilevel Inheritance:
Multilevel inheritance occurs when a class (child class) inherits from another child class,
forming a chain of inheritance.
Example:
class Animal {
void sound() {
void sound() {
System.out.println("Dog barks");
void sound() {
System.out.println("Puppy whines");
In this example, Puppy inherits from Dog, and Dog inherits from Animal, forming a
multilevel inheritance hierarchy.
3. Hierarchical Inheritance:
Hierarchical inheritance occurs when multiple classes inherit from the same parent class.
Example:
class Animal {
void sound() {
void sound() {
System.out.println("Dog barks");
void sound() {
System.out.println("Cat meows");
}
public class Test {
Here, both Dog and Cat inherit from the same parent class Animal.
Java does not allow multiple inheritance through classes due to ambiguity issues, but it
supports multiple inheritance through interfaces. A class can implement multiple interfaces,
thus inheriting behaviors from multiple sources.
Example:
interface Animal {
void sound();
interface Movable {
void move();
System.out.println("Dog barks");
}
System.out.println("Dog runs");
In this example, Dog implements both Animal and Movable interfaces, allowing it to inherit
behaviors from both.
5. Hybrid Inheritance:
Java does not support hybrid inheritance directly through classes, but a class can inherit from
multiple interfaces, which can be considered as hybrid inheritance through interfaces.
interface Animal {
void sound();
interface Movable {
void move();
@Override
System.out.println("Dog barks");
@Override
System.out.println("Dog runs");
Advantages of Inheritance:
Code Reusability: Inherited methods and properties from the parent class can be
reused in the child class.
Method Overriding: Allows for the modification of parent class methods in the child
class.
Extensibility: New functionality can be added without altering the parent class.
Conclusion:
Inheritance helps in organizing and managing the relationships between classes by creating
hierarchies and providing a mechanism for code reuse. While Java allows inheritance through
classes and interfaces, multiple inheritance is only supported through interfaces to avoid
ambiguity.
Yes, you can override a method in a subclass to throw an exception, but there are rules:
Example:
1. Valid:
2. class Superclass {
3. void myMethod() {
4. System.out.println("Superclass method");
5. }
6. }
7.
8. class Subclass extends Superclass {
9. @Override
10. void myMethod() throws IOException { // Throws a checked exception
11. System.out.println("Subclass method");
12. throw new IOException("Exception");
13. }
14. }
15. Invalid (throws an incompatible exception):
16. class Superclass {
17. void myMethod() {
18. System.out.println("Superclass method");
19. }
20. }
21.
22. class Subclass extends Superclass {
23. @Override
24. void myMethod() throws SQLException { // Compilation error, SQLException is
not compatible
25. System.out.println("Subclass method");
26. throw new SQLException("Exception");
27. }
28. }
So, you can throw an exception in the subclass, but it must follow these rules.
What is JVM?
JVM stands for Java Virtual Machine. It is a part of the Java Runtime Environment (JRE)
that is responsible for executing Java bytecode. The JVM converts the compiled Java code
(bytecode) into machine code so that it can run on any operating system and hardware
platform. The JVM is what allows Java to be platform-independent, meaning the same Java
program can run on different devices or operating systems without modification.
Functions of JVM:
1. Loading of Class Files: The JVM loads compiled .class files (bytecode) into memory
for execution.
2. Bytecode Verification: The JVM verifies the bytecode to ensure that it adheres to the
Java language specification and does not perform illegal operations.
3. Execution of Bytecode: The JVM executes the bytecode by converting it into
machine code specific to the underlying operating system and hardware architecture.
This is done using an interpreter or Just-In-Time (JIT) compiler.
4. Memory Management: The JVM manages memory through garbage collection,
automatically freeing up memory from objects that are no longer needed.
5. Exception Handling: The JVM handles exceptions (errors in the program) by
providing mechanisms like try-catch blocks.
6. Security: The JVM includes a security manager that controls access to system
resources like file I/O or network connections, ensuring that Java programs run in a
safe environment.
Java is often described as a platform-independent language, which means that Java code
can run on any platform that has a compatible JVM. However, it might seem confusing
because the JVM itself is platform-dependent.
To clarify:
Java is platform-independent because the Java source code is compiled into
bytecode. This bytecode can be run on any platform with a JVM.
The JVM is platform-dependent because it is specific to the underlying operating
system and hardware. Each operating system has its own JVM implementation
(Windows, Linux, macOS, etc.).
So, Java programs are platform-independent at the source code and bytecode level, but they
require a JVM that is specific to the platform they are running on.
In summary:
JVM: Converts bytecode into machine code and provides the runtime environment
for Java applications.
Platform Independence: Java code can run on any platform with a compatible JVM,
making it platform-independent at the source code level but platform-dependent at the
JVM level.
The garbage collector in Java is an automatic memory management system that reclaims
memory from objects that are no longer in use, preventing memory leaks and optimizing
resource utilization.
1. Identification of Unused Objects: The garbage collector identifies objects that are
no longer referenced by any part of the program (i.e., unreachable objects).
2. Marking: It marks all reachable objects, starting from active references (like
variables, method parameters, etc.).
3. Sweeping (Deletion): Unreachable objects that were not marked are removed from
memory, freeing up space.
4. Compacting (Optional): The memory may be reorganized to eliminate
fragmentation and optimize free space.
This process runs automatically in the background, and developers don’t need to manually
manage memory.
In summary, Java is compiled into bytecode, and then interpreted (or compiled to machine
code) by the JVM for execution.
Each class with methods that can be overridden by subclasses has a corresponding
VMT.
When a method is called on an object, the JVM checks the object's class's VMT to
determine which method implementation to invoke, especially when method
overriding is used.
This allows for dynamic method calls at runtime, depending on the object's actual
type, not its reference type.
Command-Line Arguments
Command-line arguments allow users to pass inputs to a Java program when it is executed
from the command line.
These arguments are passed as an array of strings (String[] args) to the main method
in Java.
They provide a way to customize the behavior of a program without modifying its
source code.
Example:
If run with the command java CommandLineExample Hello World, it will print:
Argument 1: Hello
Argument 2: World
Thread-Like Cycle
A thread-like cycle refers to the lifecycle of a thread in Java, which defines the different
states a thread can be in during its execution:
The thread cycle involves these transitions based on the thread's state and execution flow,
controlled by the JVM and the operating system.