Java Core

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Exception:

https://www.javatpoint.com/difference-between-throw-and-throws-in-java

Rules for exception handling with method overriding in Java:

1) If super class method has not declared any exception using throws
clause then subclass overridden method can't declare any checked
exception but it can declare unchecked exception with the throws clause.

2) If super class method has declared a checked exception using throws clause
then subclass overridden method can do one of the three things.

Sub-class can declare the same exception as declared in the super-class


method.
Subclass can declare the subtype exception of the exception declared in
the super class method. But subclass method can not declare any
exception that is up in the hierarchy than the exception declared in the
super class method.

3) Subclass method can choose not to declare any exception at all.

Throw and Throws:

The throw keyword is used inside a function. It is used when it is required to


throw an Exception logically.

The throws keyword is used in the function signature. It is used when the function
has some statements that can lead to exceptions.

Throw can throw only one exception at a time.


Throws can throw multiple exception at a time.

throw keyword is followed by the instance variable.


throws keyword is followed by the exception class name.

throw is used to throw runtime/unchecked exception only.


throws is used to throw compiletime/checked exception only.

Interface::

Interface is required to solve diamond problem/multiple inheritance.

If a class and interface(default method) is having same method, then method of


class will have more priority.

We can call default method of an interface by, Test.super.m1();

Dafault method::

It allows us to add more funcationaly to interface without breaking older


implemenatations.
No need to overirde if not required unlike a normal absract method of interface.
Instance Initializer::

Block is used to initialize the instance data member. It run each time when object
of the class is created.

**Instance intializer block is invoked at the time of object creation. The java
compiler copies the instance initializer block in the constructor after the first
statement super(). So firstly, constructor is invoked.

** The instance initializer block comes in the order in which they appear.

class A{
A()
{
super();
{Instance block}
{constructor code}
}
}

***If using Set with primitive, it will not save duplicate, but if we use non
primitve, it will save duplicates also, so needto override hashcode and equals
method.

** Sychronized means, multiple thread will not be allowed to access a particular


object at a time.
** Thread safe means, it will not allow 2 threads to work on a single resources at
a time.

*** Marker/markup interface

It is an empty interface (no field or methods). Examples of marker interface are


Serializable, Cloneable and Remote interface. All these interfaces are empty
interfaces.
It provides run-time type information about objects, so the compiler and JVM have
additional information about the object. A marker interface is also called a
tagging interface.
Ex: Cloneable, Serializable, Remote interface.

** method hiding

Method hiding can be defined as, "if a subclass defines a static method with the
same signature as a static method in the super class, in such a case, the method in
the subclass hides the one in the superclass." The mechanism is known as method
hiding. It happens because static methods are resolved at compile time.

You might also like