Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Total modifiers Need Of Modifiers

public, <default>, protected, private, final, abstract, Modifiers


1. To Provide some information about class to JVM.
static, synchronized, native, strictfp, transient, volatile. 2. Information Includes:
a) Class can be accessible from anywhere or not.
Class Modifiers Member Modifiers b) Child class creation is possible or not.
c) Instantiation is possible or not.

For top Class For inner Class Final Instance Variable


For Methods 1.Varied from object to object.
For Variables
2.Must initialize explicitly before
5 Modifier 8 Modifier
constructor completion.
public, <default>, final, abstract, strictfp. + private, protected, static.
All other Modifiers for Variable
Public Class Public, <default>, private, protected, final, Final Static Variable
Final Method 1.Sinble Copy for every object.
Accessible from anywhere. static, transient, volatile.
Child can’t Override final method. 2.Must initialize explicitly before
Not Applicable Modifiers: abstract, native,
Default Class synchronized, strictfp. class loading completion.
Abstract Method
Accessible from current
1. If we don’t know implementation
package only. Final local Variable
then write it as Abstract. Final Variable
1.Before using local variable perform
It’s Value cannot be changed.
Final Class initialization explicitly.
strictfp Method
We can’t create child class.
1.All floating-point Calculation will
All other Modifiers for Methods
follow IEEE754 Standard.
Abstract Class Public, <default>, private, protected, final, abstract, static, synchronized, native, strictfp.
We can’t create objects. Not Applicable Modifiers: transient, volatile.
Instantiation is not possible. Protected Method
1.Current package Access + Only
Childs from outside package. A little Explanation
Strictfp Class Native: Method which can be implemented in non-java are called native or foreign methods.
In this, every concrete (Only For Methods)
method will follow IEEE754 Static Method
Synchronized: Method or Block declared as synchronized allow only one thread at a time to
Standards and give 1. Can Access from Instance and
operate on it. (Remove Data Inconsistency Problem) (For Method and Blocks).
platform independent static areas. But can’t access
Transient: During serialization JVM ignore original value of transient variable & save default
result. instance members from static areas.
value. (For Security Purpose) (For Variables).
2. But C
Volatile: A separate copy of volatile variable is created for every thread and all modification is
done on respective copy by respective thread. (Overcome Inconsistency Problem) (Increase
Core Points in Comparisons.
complexity and creates performance problem) (For Variables).
Abstract vs static: For static methods compulsory implementation should be
Final: Missing key benefits of OOPS. But increases Security. (Never recommended for Methods
available where as for Abstract methods implementation is not compulsory.
and classes).
Abstract vs native: For native methods implementation is already available
Abstract: Highly Recommended to use. Promotes Several OOPS features. Provides guideline to
whereas abstract methods implementation is not compulsory. (Illegal
Child class which methods they have to implement compulsory. If a class has at least one
Combination).
abstract method must declare it as Abstract Class.
Native vs strictfp: We can’t declare native method as strictfp because there is no
guarantee that native language follow IEEE754 Standard.
Definition Declaration And Implementation
1. Service Requirement 1. Methods should be implemented Method Naming Conflicts
Methods If 2-interfaces have methods with
Specification. (SRS) otherwise declare it as Abstract Class.
1. By Default Methods are 1. same signature and return type then one implementation
2. Contract Between Client 2. Method should not be private,
Public and Abstract whether is enough.
and Service Provider. synchronized, final, static, native,
we declare it or not. 2. Same name and diff Arguments is called overloading.
3. 100% Pure Abstract Class. strictfp.
3. Same Signature and different return type then impossible
to implement such methods.
Variables
1. By Default Variables are
Public, Static and Final Variable Naming Conflicts
because they are available to can resolve problem by using interface name along with it.
each class and no one can
change them.
Interfaces

Adaptor Class Marker/Tagged/Ability Interface Implements vs Extends


1. Instead of implementing interface if we extend 1. An interface which doesn’t contain any methods and by 1. class ->extends -> only One class.
adaptor class then have to provide implementation implementing that interface if our objects will get some ability 2. class -> implements -> Any no. of Classes.
only for required methods not for total methods of such type of interfaces are called Marker Interface. 3. interface -> extends -> Any no. of interfaces.
interface. 2. Example: By implementing Cloneable interface our objects 4. Child Class -> implements -> Any no. of interface.
2. Reduce length of code and increase readability. can able to produce exactly duplicate cloned objects.
3. Example: we can develop a servlet either by
implementing servlet interface or by extending
GenericServlet (Adaptor Class) Interface Abstract Class Concrete Class
If we don’t know If we know about partial If we have full
implementation go for implementation go for Abstract Class. implementation and ready
interface. Example: Servlet Example: GenericServlet. to provide services go for
concrete class.
Every method is public and Inside abstract class other than Example:
abstract. abstract method can be created like MailSendingService
final method.
Every variable is public Need not be public static final
static final
Initialization is required. Initialization is not compulsory
We can’t create constructor Constructor can be created.
inside it.
Encapsulation
Data Hiding Abstraction
1. just highlight the set or 1. Grouping Function and (Static Polymorphism) Overloading
1. Outside person Can’t touch
services by hiding internal Corresponding data into a 1. Same name but different arguments.
data directly.
Single Capsule. 2. Overloading is based on reference type so it
2. Private keyword is implementation.
2. i) Java Class is is also called Static Polymorphism/Compile
responsible. 2. Can Achieve using abstract
class and interfaces. encapsulated component. time polymorphism/ Early Binding.
ii) Package is encapsulated Automatic promotion
3. Enhancement becomes
mechanism. 1. If resolution is matched then that will be
easy because outside person
3. Encapsulation = Data considered otherwise
can’t change internal
hiding + Abstraction. Byte-> short, char-> int-> long-> float ->double
implementation.
4. Tightly Encapsulated Class:
Where every variable is
private.

Is-A Relationship (Dynamic Polymorphism) Overriding


1. Using extends keyword we OOPS 1. As it is a facility for child so it is not based on
can implement Is-A reference but based on dynamically created
Relationship. object.
2. Child reference can’t store 2. Facility for child to redefine Parent method.
Inheritance Static Polymorphism 3. It is called Runtime Polymorphism/Dynamic
parent object. Reusability
3. Parent reference can store polymorphism/Late Binding.
child object but can not call Rules for Overriding
child specific methods.
Reusability in 1. Same signature.
modifies way
2. Not applicable for private/final method.
Overriding w.r.t. Static Methods
Polymorphism 1. Static Method Can’t override as Static
Has-A Relationship Method Signature
Methods and vice versa.
1. new-keyword is used for it. Multiple/Cyclic inheritance
1. not allowed. 2. If both are Static then possible but it is called
2. Advantage is reusability.
Method hiding (Static Polymorphism).
3. Increase Dependency and 2. Can be achieved by
Overriding w.r.t. var-arg Methods.
maintenance problem. interfaces.
1. It is called Overloading not overriding.

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.

You might also like