Java OOPs surrounding with answers
Java OOPs surrounding with answers
Constructor:
1. How will you call parameter less constructor, write one eg
First we should create object for that class.
Eg :
public class Test{
public Test(){
System.out.println(“Welcome”);
}
}
public class MyClass{
public static void main(String[] args){
Test t = new Test();
}
}
7. How will you call parent class constructor, which doesn’t have parameters
The JVM automatically creates a super call in child class to access parent
class constructor. We can call parent class constructor which doesn’t have
parameters by creating object for the child class.
Eg:
class Program{
public void m1(){
System.out.println(“Programming”);
}
}
class Computer extends Program{
public void m2(){
System.out.println(“Computer”);
}
}
public class MyClass{
public static void main(String[] args){
Computer c = new Computer();
c.m2();
c.m1();
}
}
8. How will you call parent class constructor, which have parameters
We can call parent class constructor which has parameters by using super
call with values in child class and creating object for the child class .
Eg :
class M1{
public M1(int a){
System.out.println(“M1”);
}
}
class M2 extends M1{
public M2(){
super(10);
System.out.println(“M2”);
}
}
public class MyClass{
public static void main(String[] args){
M2 m = new M2();
}
}
9. How will you call same class constructor from other constructor
Constructor chain / constructor execution sequence : it is the ability to
call a constructor in another constructor.
Using this() keyword we can call same class constructor from other constructor
(using super() keyword we can call parent class constructor from child class)
Eg :
class M1{
public M1(int a){
System.out.println(“M1”);
}
}
class M2 extends M1{
public M2(){
super(10); //super() is used to call the constructor of M1
System.out.println(“M2-1”);
}
public M2(char c){
this(); // this() is used to call the same class constructor
System.out.println(“M2-2”);
}
}
public class MyClass{
public static void main(String[] args){
M2 m = new M2(‘*’);
}
}
10. Will it give compile time error if we give return type to constructor
No, because it treats it as a normal method.
13. public class A { private A() {} } . what is the correct way to create object
for this class?
A a = new A();
15.public class A
{
public A(int i) {}
}
public class B extends A
{
public B(int i) {}
}
Will the above code compile successfully?
No because we have to declare super keyword with an integer value in
child class because the constructor of parent class has an integer value.
Encapsulation:
1. Give me one scenario example where you have implemented encapsulation.
Students must tell one project scenario to explain the encapsulation.
public class Doctor{
public String name;
public int exp;
public Doctor(String name, int exp){
this.name = name;
this.exp = exp;
}
public void suggestMedicine(String disease){
//return medicine name based on disease
}
}
Here all the logically related data and functions of doctor is stored in Doctor
class.
2. What is the use of encapsulation?
It is used to bind logically related data and functions in a common related
place.
3. which oops feature do you use to achieve data security [or] data hiding
Encapsulation
4. How will you hide data from the external world using encapsulation? Hint :
Students must talk about bean class.
By using bean class data security can be provided since all the variables
of bean class are private and each variable has a getter and setter method.
7. Write setter and getter methods for variable private int sno;
public interface MyInterface{
private int sno;
public void set sno(int sno){
this.sno = sno;
}
public void get sno(){
return this.sno;
}
}
Abstraction:
1. How will you achieve abstraction in java?
Abstraction is achieved by using the abstract keyword for abstract class
and interfaces.
Inheritance:
1. Can I inherit private variables of parent class in child class?
No
13. How will you achieve multiple inheritance in java? Show one eg?
Implementing multiple interfaces in a class
Eg:
class A{
//java code
}
interface One{
//java code
}
interface Two{
//java code
}
class B extends A implements One, Two{
//java code
}
2. multi level
A
B
C
3. hierarchial
A
B C D
4. multiple
A B
5. hybrid
A
B C
17. Trainer can ask any coding based question on inheritance find output
Polymorphism:
1. Give one example for static polymorphism
Method Overloading
Eg:
class Sample{
public int m1(){
return 10;
}
public int m1(int x){
return 1000;
}
public int m1(String y){
return 10;
}
public int m1(int z, int r){
return 150;
}
}
Method overloading
1. If 2 methods have the same name, same parameters, but different return type,
is it overloading or error?
Error because it doesn’t participate in method overloading coz of same
parameters and it considers it as normal methods, but normal methods can have
the same name but cannot have same parameters hence error
Method overriding:
1. Can i override private methods of parent class in child class
No, private or static methods cannot be overridden.
4. If I don't want to override a parent class method in child class, what should I
do?
1. Using a static method
2. Using private access modifier
3. using default access modifier
4. using the final keyword method
5. What is the difference between private method and final method (as we can’t
override both methods in child class)?
Private Members Final
1. Even by creating object you 1. By creating object you can
cannot access it outside the access it outside the class
class 2. It can participate in
2. It cannot participate in inheritance (only methods)
inheritance
Final class cannot be
inherited but in normal class
final methods can be
inherited
7. How do you call parent class over ridden method from child class method.
eg.
Using super() keyword
Eg :
class A{
public void fun(){
System.out.println(“Base function”);
}
}
class B extends A{
public void fun(){
super.fun();
System.out.println(“Derived function”);
}
}
public static void main(String[] args){
B b = new B();
b.fun();
}
2. How will you access non static variables [or] non static methods of a class?
Show one eg
Accessed using object name
Eg:
class Demo{
public int x = 20;
public int m1(){
return 10;
}
}
public static void main(String[] args){
Demo d = new Demo();
System.out.println(d.x);
d.m1();
}
3. How will you access methods (which have bodies) of an abstract class?
We need to extend and use child class object variable to access abstract
class methods.
5. When will you use an abstract class, give one scenario with code
(same question can be asked as what is the use of abstract class)
1. Used when we have to share common functionalities with related
classes
2. It enforces child class to give body to a particular method
3. Avoids code duplication
Eg :
public abstract class A{
public abstract void f1();
public abstract int f2(int x);
}
public class B extends A{
public void f1(){
System.out.println(“Hi);
}
public int f2(int x){
System.out.println(“Bye”);
return 100;
}
}
public static void main(String[] args){
B b = new B();
b.f1();
b.f2(10);
}
6. When will you use an abstract method, give one scenario with code
(same question can be asked as what is the use of abstract method)
Using an abstract method in the code saves time. We can call the abstract
method wherever the method is necessary. Abstract class avoids the process of
writing the same code again. Abstraction is achieved by abstract methods.
Eg:
public abstract class A{
public abstract void f1();
public abstract int f2(int x);
}
7. Can I achieve complete or full abstraction with the help of abstract class?
Why if not?
No because it can also have concrete methods and also if there are even
zero abstract methods the class can be made as an abstract class.
9. Take a parent class with few abstract methods, ask student to inherit into a
child class. Ask student to call the methods with object.
Eg:
public abstract class A{
public abstract void f1();
public abstract int f2(int x);
}
public class B extends A{
public void f1(){
System.out.println(“Hi);
}
public int f2(int x){
System.out.println(“Bye”);
return 100;
}
}
public static void main(String[] args){
B b = new B();
b.f1();
b.f2(10);
}
10. Trainer can ask any coding question on abstract method or class
Interface:
1. What is the use of interface (same question can be asked as when will you
use interface) give one scenario example with code
Interfaces lets us share common functionalities with unrelated classes.
Eg:
public interface I{
public void fun1();
public int fun2(int x);
}
class Imp implements I{
public void fun1(){
System.out.println(“Hi”);
}
public int fun2(int x){
Return x;
}
}
public static void main(String[] args){
Imp i = new Imp();
i.fun1();
int y = i.fun2(10);
System.out.println(y);
}
8. How will you implement an interface in a class and how will you call
methods.
Refer code in answer 1
Access specifier:
1. Can I access protected variables in other packages?
We can protected variables in subclasses of other packages but not in
different classes of other packages
}
}
Exceptions:
1. What is checked exception, give one eg.
Checked exceptions are exceptions which are to be handled by
programmer using try – catch block or throws clause
Eg for checked exceptions are: SQL Exception, IOException like
FileNotFoundException, SocketException etc
4. If I use throws, and if an exception occurs in the current method, then what
will happen. Who is going to handle that exception?
When a method declares that it throws an exception, it is not required to
handle the exception. The caller of the method that throws exceptions is
required to handle the exception (or throw them to its caller and so on) so that
the flow of the program is maintained
7. What happens if an exception occurs in the finally block? Will the finally
block be executed?
Yes a finally block is always executed whether the exception has
occurred or not.