Java Recall Notes 1654687527
Java Recall Notes 1654687527
Dynamic Polymorphism
Inheritance vs Polymorphism Overriding vs Method Hiding Static Control Flow Instance Control Flow
1. Inheritance enables code reuse. 1. Overriding->non-static methods. 1. Identification of Static Members 1. Identification of Instance Members. (Parent to child)
2. Polymorphism enables same Method hiding -> Static methods. 2. Static variable assignments and 2. Instance variable assignments and block. (Parent)
functionality in modified way. 2. Overriding -> Runtime polymorphism static block. 3. Constructor. (Parent).
Method hiding-> Compiler Time ... 3. main method. 4. Now Same for Child.
Default Exception Handling
Exception Def 1. Method in which it is raised create exception object.
Unwanted, unexpected event that disturbs normal Excepion Handling 2. Exception object handovers to JVM.
flow of application execution. 3. JVM identifies caller method up to main(). If there is no
Example: FileNotFoundException. handling code then JVM handovers the object to default
exception handler. It is a part of JVM. It terminates
What is Exception Handling program abnormally with Description and StackTrace.
1. Defining an alternative way to continue the rest of Exception Hierrarchy
the program normally is called exception handling. Customized Exception Handling by try catch blocks
Example: If America file not found than use local file. 1. write risky code inside try block and respective handling
code inside catch block.
1. Caused by program 2. Method to print Exception Information:
Throwable class
and are recoverable. PrintStackTrace(), toString(), getMessage().
2. Example: FileNotFound 3. If multiple catch blocks are there then Order must be
Errors
Exception from child to parent.
1. Not Caused by our
Checked UnCheck program and these are due Coupling
to lack of system resources. 1. Definition: The degree of dependency between the components.
ed
1. Checked by compiler for 1. Not checked by compiler. 2. Not Recoverable. 2. Tight Coupling: When all classes are connected together. Tight Coupling is
smooth execution. 2. Example: All 3. Example: never recommended. (Disadvantages: Reduce maintainability, without effecting
2. Example: FileNotFound RuntimeException OutOfMemoryError and remaining component we can’t modify any component, Enhancement becomes
3. Compiler will check whether (ArithmaticException, StackOverflow difficult, doesn’t promote reusability of Code).
we’re writing handling code or NULLPointerException) and 3. Loose Coupling is recommended.
not. If not then we’ll get C.E. Errors. Type Casting Cohesion
4. Fully Checked: If all its child 1. Parent class reference can 1. Definition: For every component we have to define a clear well-defined
classes are checked. (All hold child class object. functionality. Such component is said to be high cohesion.
IOException, 2. Here, we’re not creating 2. Example: MVC framework follow high cohesion. (Advantage: Improve
InterruptedException) completely separate maintainability, promotes reusability, High cohesion is highly recommended,
Other Important Without effective any component we can modify any other component).
5. Partial Checked: some of its independent object just we
child classes are unchecked. creating another type of Throw keyword
(Exception class) reference for existing object. 1. we can create exception object explicitly and we can handover that object to
the JVM manually for this we have to use throw keyword.
Final vs Finally vs finalize() Throws keyword
Finally block: Specialty of finally block is it will be executed always whether 1. we can use throws keyword to delegate responsibility of exception handling
exception raised or not and handled or not handled. to caller.
2. System.exit(0) – dominate -> finally block – dominate -> return statement. 2. Required only for checked exception.
3. Finally block is mainly used for cleanup code. 3. Use to convince compiler. It doesn’t prevent abnormal termination.
4. Finally is block associated with try catch block. 4. try catch is recommended over throws.
Final: It is modifier used for variable, class and method for different security 5. Can use throws keyword for methods and constructor but not for classes.
purposes. 6. Only use for throwable types.
Finalize(): A method always called be garbage collector just before destroying an Top 8 Exeption names
object to perform cleanup activity. NullPointer, programmatic, ArrayIndexOutOfBound, NoClassDefFoundError,
2. finally block is recommender to use in place or finalize(). StackOverFlow, NumberFormat, AssertionError, ExceptionInInitializerError.