3.OOPs & Exceptions
3.OOPs & Exceptions
OOPS CONCEPTS
There is no copy constructor in java. However, we can copy the values from one
object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They
are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
2. Can you call the base class method without creating an instance?
Yes, you can call the base class without instantiating it if:
It is a static method
The base class is inherited by some other subclass
3. What is the Inheritance?
Inheritance is a mechanism by which one object acquires all the properties and
behaviour of another object of another class. It is used for Code Reusability and
Method Overriding. The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes. When you inherit from an existing
class, you can reuse methods and fields of the parent class. Moreover, you can
add new methods and fields in your current class also. Inheritance represents the
IS-A relationship which is also known as a parent-child relationship.
Single-level inheritance
4. What is composition?
Holding the reference of a class within some other class is known as composition.
When an object contains the other object, if the contained object cannot exist
without the existence of container object, then it is called composition. In other
words, we can say that composition is the particular case of aggregation which
represents a stronger relationship between two objects. Example: A class contains
students. A student cannot exist without a class. There exists composition
between class and students.
The method must have the same name as in the parent class.
The method must have the same signature as in the parent class.
Two classes must have an IS-A relationship between them.
No, you can’t override the static method because they are the part of the class,
not the object. It is because the static method is the part of the class, and it is
bound with class whereas instance method is bound with the object, and static
gets memory in class area, and instance gets memory in a heap.
9. What is aggregation?
Aggregation can be defined as the relationship between two classes where the
aggregate class contains a reference to the class it owns. Aggregation is best
described as a has-a relationship. For example, The aggregate class Employee
having various fields such as age, name, and salary also contains an object of
Address class having various fields such as Address-Line 1, City, State, and pin-
code. In other words, we can say that Employee (class) has an object of Address
class.
If we make any class final, we can’t inherit it into any of the subclasses.
In Java, a class cannot extend more than one class. Therefore following is illegal
Example
Consider a case where class B extends class A and Class C and both class A and
C have the same method display().
Now java compiler cannot decide, which display method it should inherit. To
prevent such situation, multiple inheritances is not allowed in java.
A final variable, not initialized at the time of declaration, is known as the final
blank variable. We can’t initialize the final blank variable directly. Instead, we
have to initialize it by using the class constructor. It is useful in the case when the
user has some data which must not be changed by others, for example, PAN
Number.
When the same method has to perform different tasks depending on the object
calling it.
When you need to be overridden in its non-abstract subclasses.
This subclass constructor has a super keyword in the first line that calls
constructor of an abstract class. Thus, the constructors of an abstract class are
used from constructor of its subclass.
If the abstract class doesn’t have constructor, a class that extends that abstract
class will not get compiled.
EXCEPTION HANDILING
36. How the exceptions are handled in java? OR Explain exception handling
mechanism in java?
Exceptions in java are handled using try, catch and finally blocks.
try block : The code or set of statements which are to be monitored for
exception are kept in this block.
catch block : This block catches the exceptions occurred in the try block.
finally block : This block is always executed whether exception is
occurred in the try block or not and occurred exception is caught in the
catch block or not.
38. Can we write only try block without catch and finally blocks?
No, It shows compilation error. The try block must be followed by either catch
or finally block. You can remove either catch block or finally block but not
both.
39. There are three statements in a try block – statement1, statement2 and
statement3. After that there is a catch block to catch the exceptions occurred
in the try block. Assume that exception has occurred in statement2. Does
statement3 get executed or not?
No. Once a try block throws an exception, remaining statements will not be
executed. control comes directly to catch block.
When you are keeping multiple catch blocks, the order of catch blocks must be
from most specific to most general ones. i.e sub classes of Exception must come
first and super classes later. If you keep super classes first and sub classes later,
compiler will show unreachable catch block error.
Checked Exceptions
Checked exceptions are known to compiler i.e they are the exceptions that are
checked at compile time. Checked exceptions are also called compile time
exceptions, because they can be known during compile time.
Unchecked exceptions are not known to compiler. They are the exceptions that
are not checked at compile time, because they occur only at run time. That’s why
these exceptions are also called run time exceptions.
The exceptions which occur at run time are called as run time exceptions. These
exceptions are unknown to compiler. All sub classes of
java.lang.RunTimeException and java.lang.Error are run time exceptions. These
exceptions are unchecked type of exceptions. For example,
NumberFormatException, NullPointerException, ClassCastException,
ArrayIndexOutOfBoundException, StackOverflowError etc.
Checked exceptions are the exceptions which are known to compiler. These
exceptions are checked at compile time only. Hence the name checked
exceptions. These exceptions are also called compile time exceptions. Because,
these exceptions will be known during compile time.
Unchecked exceptions are those exceptions which are not at all known to
compiler. These exceptions occur only at run time. These exceptions are also
called as run time exceptions. All sub classes of java.lang.RunTimeException
and java.lang.Error are unchecked exceptions.
46. Can we keep the statements after finally block If the control is returning
from the finally block itself?
No, it gives unreachable code error. Because, control is returning from the
finally block itself. Compiler will not see the statements after it. That’s why it
shows unreachable code error.
47. Does finally block get executed If either try or catch blocks are returning the
control?
Yes, finally block will be always executed no matter whether try or catch blocks
are returning the control or not.
Exceptions raised in the try block are handled in the catch block. If it is unable
to handle that exception, it can re-throw that exception using throw keyword. It
is called re-throwing an exception.
Because finally block is always executed whether exceptions are raised in the
try block or not and raised exceptions are caught in the catch block or not. By
keeping the cleanup operations in finally block, you will ensure that those
operations will be always executed irrespective of whether exception is
occurred or not.
I/O EXCEPTIONS
54. What is the use of the PrintStream class in Java IO? (answer)
PrintStream is used to write data on Console, for example, System.out.println(),
here out is an object of PrintStream class and we are calling println() method from
that class.