Friday 7 November 2014

OOP'S Concepts(Note: Interviewer is asking more Questions on oop's. I will explain later in detail )

1) Abstraction & Encapsulation
2) Polymorphism
3) Inheritance
4) Abstract Class
5) Interface

Q) What is Abstraction?
A) Hiding the implementation and providing the service is called Abstraction.

Q) What is the Encapsulation?
A) Binding the data along with its related functionalities is called Encapsulation.

Ex:
class Account {
private double balance;

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}
}
Advantages:
1) Security
2) Easy Enhancement
3) Improve Maintainability

Q) What is Polymorphism ?
A) This has been derived from Greek word. 'Poly' means Many, Morphism means 'Forms'. Totally one name with multiple forms is called Polymorphism.

Types of Polymorphism.
  • Static Binding   or  Compile Time  Polymorphism or Early Binding.
Ex: Method overloading
  • Dynamic Binding  or Run time Polymorphism  or Late Binding.
Ex : Method overriding.

Q) What is Method over loading .
A) Defining multiple Methods with same name, with different arguments in a class is nothing but over loading.

Ex:

public class MethodOverLoading {

public static void main(String[] args) {

MethodOverLoading overLoading= new MethodOverLoading();

overLoading.yogi(10);
overLoading.yogi(10,20);
overLoading.yogi(10,20f);


}
public void yogi(int i)
{
System.out.println("org1"+i);
}
public void yogi(int i, int j)
{
System.out.println("org2"+i+"org2"+j);
}
public void yogi(int j, double k)
{
System.out.println("org3"+j+"org4"+k);
}
public void yogi(int j, double k, float f)
{
System.out.println("org5"+j+"org6"+k);
}

}

Q) What is the Method overriding?
A) Defining a method in Sub Class with the same signature of the Super Class  or  defining a method in the Super Class with the same signature of the Sub Class is called a Method Overriding

Ex:

public class OverRidingEx {

public static void main(String[] args) {

Z z1= new Z();
z1.one();
X x1= new X();
x1.one();
//Z zz1= new x(); //compilation error
X xx1= new Z();
xx1.one();

}

}

class X
{
public void one()
{
System.out.println("super class");
 }
}
 
class Z extends X
{
public void one()
{
System.out.println("sub class");

}

}

out-put
-------
sub class
super class
sub class





Q) What is  Inheritance?

A) Inheritance is the concept of getting the properties of one class to another class or one object to another object is  called Inheritance.

Q) Why multiple Inheritance doesn't support java.
A)

public class InheritanceEx {

public static void main(String[] args) {

}

}

class A extends B,C
{
public void M1()
{
System.out.println("class A");}
}

class B
{
public void M1()
{
System.out.println("class B");}
}

class C
{
public void M1()
{
System.out.println("class C");}
}

Explanation:
1. Multiple Inheritance is not supported in java just because of the Violation of super() keyword.
When Ever you called the method/constructor of super class at that time JVM got confused,it cant decide which method you are calling
Q) What is Abstract class?
A) Partially implemented and Partially unimplemented class is Abstract class. That means Abstract class contains, abstract methods as well as implemented(concrete) methods.

Ex:
abstract class AbstractClassEx1
{
    AbstractClassEx1(){
       
    }
public abstract void m1();
public void m2()
{
System.out.println("yogi");   
}
}

Q) What is Interface?
A) Fully unimplemented structure called Interface. That means Interface contains only Abstract methods and  variables.

Ex:
public interface InterFaceEx {
    int j=20;
    public static final int i=10;
    public void m1();
    public void m2();
    public void m3();
   
}
Q) What is the class?
A) Fully implemented structure is called a class 

public class ClassEx {

    public static void main(String[] args) {
   
        ClassEx ex= new ClassEx();
        ex.m1();
    }

    public void m1()
    {
    System.out.println("methos imp");   
    }
}
Q) What is Object?
A)  Instance of a class is called object.











No comments:

Post a Comment