-->

Java OOPS Interview Question Part 1

Posted by Admin on
Q:- What is OOPS (Object Oriented Programming Structure ?)
Ans:-OOPS is a concept or paradigm to design a program using objects and classes. It simplifies the software development and maintenance by providing some concept.

• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

Q:-What are the advantage of OOPs over Procedure Oriented Programming language.
• OOPS makes development easier whereas in procedure oriented programming language it is not easy to manage if code grows as project grows.
• OOPS provides data hiding whereas in procedure oriented programming language a global data can be accessed from anywhere.
• OOPS provides ability to simulate real world event much more effectively.

Q:- What is the difference between Object Oriented Programming language and Object based programming language. 
Ans:-Object based programming language follows all the features of Object oriented language except inheritance .eg:- JavaScript.

Q:-What is the difference between Object and class.
Object:- A runtime entity that has state and behaviour is known as object. Object is an instance of a class. An object has three characteristic .
• State :- represent the data of object .
• Behaviour :- represent the behaviour of object. 
• Identity:- is typicaly implemented using Unique id  . The value of id is not visible to the external user but is used internally by JVM to identify each object internally.
For ex :- Pen is an object. Its name is  Reynolds , color is white etc known as state . It is used to write so writing is its behaviour .

Class:- A class is a  blue print of an object . class is an abstract data type having its own characteristic and function. It is a way of binding the objects having similar characteristic and functions.
A class in java contain:-
• Data member 
• Method 
• Constructor 
• block

for ex :- fruits is a class . Mango , apple and orange are the object of this class.
import java.util.*;
import java.lang.*;
class Student{
int rollno;
String name ;
void insert(int r, String str)
{
name=str;
rollno=r;
}
void displayInformation()
{
System.out.println(rollno+""+name);
}
public static void main(String args[])
{
Student s1 = new Student ();
Student s2 = new Student ();
s1.insert(2,"ishant");
s2.insert(4,"gautam");
s1.displayInformation();
s2.displayInformation();
}
}

Assigning Object Reference Variable
Assigning an Object mean giving another name for memory. For eg:-
Box b1 =new Box();
Box b2 = b1;

Both b1 and b2 refer to the same memory

Although both b1 and b2 refer to the same object , they are not same in any other way . for ex: a subsequent assignment to b1 will simply inlock b1 from that object without affecting the or affecting b2. For ex:
Box b1 = new Box();
Box b2 = new b1;
//……

b1 = null;


Here b1 has been set null , but b2 still points to the original objects.


Constructor :-
Constructor is a special type of method that is used to initialized the state of object . Constructor is invoked at the time of object creation. It construct the values i.e  data member that is why it is called constructor.
Rules for creating constructor :-
·  It has the same name as the class .
·  It does not have return type .

Default Constructor:
Default  Constructor :- are those that have no parameter  is known as default constructor .
Example of default Constructor :-
class calculation{
    
calculation ()  // default constructor 
{
System.out.println("default Constructor called");
}
public static void main(String args[])
{
calculation C1 = new calculation(); // default constructor called on the creation of object 

}                          

Rule :- If there is no constructor in a class, compiler automatically creates a default constructor .This default constructor automatically initialize all the variable to zero(0 ) . And if we define our constructor then compiler does not create default constructor.


Parameterized Constructor :
Parametrizedd constructor are those that have parameter . 

Why do we use parameterized Constructor ?
Parameterized Constructor is used to provide different values to the distinct objects.

Example of parameterized Constructor :-

public class Student{
int rollno;
String name ;
Student(int r, String str)
{
name=str;
rollno=r;
}
void displayInformation()
{
System.out.println(rollno+""+name);
}
public static void main(String args[])
{
Student s1 = new Student (2,"ishant");
Student s2 = new Student (3, "gaurav");
s1.displayInformation();
s2.displayInformation();
}
}
Does  Constructor return any value .
Ans:-Yes, that is current class instance (You cannot use return type yet it return a value).
Does  Constructor is inherited ?
Ans:- NO
Can you make a constructor final ?
Ans:- NO

Can constructor perform any operation other than initialisation.

Ans:-Yes, object creation, starting a thread ,calling method etc.  Construtor can perform any operation you can do in the method.

What is the significance of static keyword in java ?
Ans:-There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static
because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.
Methods declared as static have several restrictions:
1.They can only call other static methods.
2.They must only access static data.
3.They cannot refer to this or super in any way. (The keyword super relates to inheritance.)
If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:
class UseStatic { 
static int a = 3; 
static int b; 
static void meth(int x) 
{ 
System.out.println("x = " + x); 
System.out.println("a = " + a); 
System.out.println("b = " + b); 
} 
static { 
System.out.println("Static block initialized."); 
b = a * 4; 
} 
public static void main(String args[]) { 
meth(42); 
} 
}
As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( )is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two staticvariables a and b, as well as to the local variable x.
Note It is illegal to refer to any instance variables inside of a static method. Here is the output of the program:
Static block initialized. 
x = 42 
a = 3 
b = 12

Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:
classname.method( )
Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object reference variables. A staticvariable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.
class StaticDemo { 
static int a = 42; 
static int b = 99; 
static void callme() { 
System.out.println("a = " + a); 
} 
} 
class StaticByName { 
public static void main(String args[]) { 
StaticDemo.callme(); 
System.out.println("b = " + StaticDemo.b); 
}  

Here is the output of this program:
a = 42
b = 99

Static  Block:-
•It is used to initialize the static data member .
•It is executed before main method at the time class loading.

Example of static block:
class StaticByName { 
static 
{
System.out.println("static block is called");
}
public static void main(String args[]
{ 
System.out.println("Main method call"); 
} 
} 

class Test {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun()
 {
 static int x= 10;  //Error: Static local variables are not allowed
 return x--;
   }
}

This Keyword :-There can be a lot of use of this variable. this is a reference variable that refers to the current object.
Usage of this keyword :-
· this can be used to refer current class instance variable .
· this can be used to invoke current class variable.
· this class can be used to invoke current class method(implicitly).
· this can be passed as an argument in the method call.
· this can be passed as an argument in the constructor call
· this can be used to return the current class instance.


Method Overloading :->
Method overloading means function with same name but different parameter .Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behaviour of the method because its name differs. So, we perform method overloading to figure out the program quickly.

Advantage of method overloading?
Method overloading increases the readability of the program. It becomes easy for us to understand the program.

There are two ways to overload the method in java
1.By changing number of arguments
2.By changing the data type

Note:  Methood Overloading is not possible by changing return type of the method.

Example of method Overloading :

 class Main{
  void sum(int a,int b)
  {System.out.println(a+b);}
  //method overloading by changing the number of argumnet  
  void sum(int a,int b,int c)
  {System.out.println(a+b+c);}
  //method oevrloading by changing the type argument 
  void sum(double a,double b)
  {System.out.println(a+b);}
  
  public static void main(String args[]){
  Calculation obj=new Calculation();
  obj.sum(10,10,10);
  obj.sum(20,20);
  obj.sum(10.5,10.5);
  }


If you find any mistake then please comment .

No comments:

Post a Comment