01-10-24 Java Day2
01-10-24 Java Day2
==========
OOPS:
========
1. Object
2. Class
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism
Object:
=======
An object is an instance of a class. When a class is defined, no memory is allocated until an object of that
class is created. Objects represent real-world entities and are used to interact with the program by
calling methods and accessing attributes defined in the class.
Pen, Car
Class:
=====
A class in Java is a blueprint or template that defines the properties (fields/attributes) and behaviors
(methods) that objects of that class will have. It acts as a user-defined data type from which objects are
created.
Encapsulation
=============
Bundling data (variables) and methods (functions) that operate on the data into a single unit (class),
and restricting access to some of the object's components.
Abstraction:
===========
Hiding the complex implementation details and showing only the essential features of an object. It is
achieved using abstract classes and interfaces.
Inheritance:
===========
Mechanism where one class (child or subclass) acquires the properties and behaviors of another
class (parent or superclass).
Polymorphism:
==============
The ability of an object to take many forms. It allows methods to do different things based on the
object it is acting upon, even if they share the same name.
Types:
Keywords:
1. abstract – Indicates that a class or method is abstract.
19. finally – Defines a block of code that always runs after a try-catch.
39. static – Indicates that a field or method belongs to the class rather than instances.
40. strictfp – Used to restrict floating-point calculations to ensure portability (since Java 1.2).
47. transient – Specifies that a field is not part of the serialized form of an object.
48. try – Starts a block of code that will be tested for exceptions.
50. volatile – Indicates that a variable may be changed unexpectedly by different threads.
Exception2 types
Hierarchy of EH class:
========================
Checkedmust be handled.
Exception Handling:
=======================
1) The term exception means exceptional condition, it is a problem that may arise during the execution
of program.
2) A bunch of things can lead to exceptions, including programmer error, hardware failures, files that
need to be opened cannot be found, resource exhaustion etc.
3) Java provides a robust and object oriented way to handle exception scenarios,known as Java Exception
Handling.
4) When the exception occurs in a method, the process of creating the exception object and handing it
over to runtime environment is called "throwing the exception".
5) Once runtime receives the exception object, it tries to find the handler for the exception. Exception
Handler is the block of code that can process the exception object. The handler is said to be "catching
the exception".
6) Note that Java Exception handling is a framework that is used to handle rùntime errors only, compile
time errors are not handled by exception handling in java.
Keywords: try,catch,finally,throw,throws.
1) The "try" keyword is used to specify a block where we should place exception code. The try block
must be followed by either catch or finally. It means, we can't use try block alone.
2) The "catch" block is used to handle the exception. It must be preceded by try block which means we
can't use catch block alone. It can be followed by finally block later.
3) The "finally" block is used to execute the important code of the program.It is executed whether an
exception is handled or not.
Throw:
1)The Java throw keyword is used to explicitly throw an exception from a method any block of code.
4) We can define our own set of conditions or rules and throw an exception explicitly using throw
keyword.
5) For example, we can throw ArithmeticException when we divide number by 5, or any other numbers,
what we need to do is just set the condition and throw any exception using throw keyword.
Throws:
1) throws is a keyword in Java which is used in the signature of method to indicate that this method
might throw one of the listed type exceptions.
2) The caller to these methods has to handle the exception using a try-catch block.
=========================
5) create try block inside that create a new exception and throw it based on some condition(throw)