0% found this document useful (0 votes)
11 views

01-10-24 Java Day2

Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

01-10-24 Java Day2

Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Scanner:

==========

How to take input from user?

Scanner sc = new Scanner(System.in);

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

Properties-Colour, Height, Weight.

Actions-Gearchange, Open windows.

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.

Eg: Blueprint, Sketch of something.

Without class we can’t create object.

An Obj is instance of class.

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:

o Compile-time polymorphism (Method Overloading): Multiple methods with the same


name but different parameters.
o Run-time polymorphism (Method Overriding): A subclass provides a specific
implementation of a method that is already defined in its superclass.

Keywords:
1. abstract – Indicates that a class or method is abstract.

2. assert – Used for testing assumptions in code (since Java 1.4).


3. boolean – Defines a boolean variable with true or false values.

4. break – Exits a loop or switch statement.

5. byte – Defines a byte (8-bit) data type.

6. case – Defines a case in a switch statement.

7. catch – Handles exceptions in a try-catch block.

8. char – Defines a character data type (16-bit Unicode).

9. class – Declares a class.

10. const – Not used in Java (reserved for future use).

11. continue – Skips the current iteration of a loop.

12. default – Specifies the default block of a switch statement.

13. do – Starts a do-while loop.

14. double – Defines a double-precision floating-point number.

15. else – Specifies the else branch in an if-else statement.

16. enum – Declares an enumerated type (since Java 5).

17. extends – Indicates that a class is inheriting from a superclass.

18. final – Marks a variable, method, or class as unchangeable or final.

19. finally – Defines a block of code that always runs after a try-catch.

20. float – Defines a single-precision floating-point number.

21. for – Starts a for loop.

22. goto – Not used in Java (reserved for future use).

23. if – Starts an if condition.

24. implements – Specifies that a class implements an interface.

25. import – Imports other Java classes or entire packages.

26. instanceof – Tests whether an object is an instance of a class or subclass.

27. int – Defines an integer data type.

28. interface – Declares an interface.

29. long – Defines a long integer data type (64-bit).

30. native – Specifies that a method is implemented in platform-dependent code (native).

31. new – Creates new objects.


32. null – Represents the null reference.

33. package – Defines a package.

34. private – Specifies access as private.

35. protected – Specifies access as protected.

36. public – Specifies access as public.

37. return – Exits a method and optionally returns a value.

38. short – Defines a short integer data type (16-bit).

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).

41. super – Refers to the superclass of an object.

42. switch – Starts a switch statement.

43. synchronized – Specifies that a method or block of code is synchronized.

44. this – Refers to the current object instance.

45. throw – Throws an exception.

46. throws – Declares exceptions that a method may throw.

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.

49. void – Specifies that a method does not return a value.

50. volatile – Indicates that a variable may be changed unexpectedly by different threads.

51. while – Starts a while loop.

ErrorCoder can get rid of it.

Exception2 types

Hierarchy of EH class:
========================

Uncheckedmethod calling may not be handle the issue.

Checkedmust 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.

EXCEPTION HANDLING KEYWORDS:


============================

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.

4) The "throw" keyword is used to throw an exception.


5) The "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies that
there may occur an exception in the method. It is always used with method signature.

Throw:

1)The Java throw keyword is used to explicitly throw an exception from a method any block of code.

2) We can throw either checked or unchecked exception in java by throw keyword

3) The throw keyword is mainly used to throw custom exceptions

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.

3) It is designed to transfer the responsibility of exception handling to it's caller.

throws------->checked (IOexception, FNFE, CNFE,SQLE)


User defined Exceptions:

=========================

1) Create the new exception class extending Exception class

2) create a public-constructor for a new class with string type of parameter

3) pass the string parameter to the super class

4) declare the exception at the method level(throws)

5) create try block inside that create a new exception and throw it based on some condition(throw)

6) write a catch block and use some predefined exceptions


7) write the optionally finally block.

You might also like