oops interview questions


1. What is OOPS?

OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

2.  Write basic concepts of OOPS?

Following are the concepts of OOPS and are as follows:.

Abstraction.

Encapsulation.

Inheritance.

Polymorphism.

3) What is polymorphism?

Polymorphism gives us the ultimate flexibility in extensibility. The abiltiy to define more than one function with the same name is called Polymorphism. In java,c++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).


When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class.


Overloading occurs when several methods have same names with


Overloading is determined at the compile time.

Different method signature and different number or type of parameters.

Same method signature but different number of parameters.

Same method signature and same number of parameters but of different type


Example of Overloading 

     int add(int a,int b)

     float add(float a,int b)

     float add(int a ,float b)

     void add(float a)

     int add(int a)

     void add(int a)                 //error conflict with the method int add(int a)


Example: Overloading



Class BookDetails{

            String title;

            String publisher;

            float price;


setBook(String title){

}

setBook(String title, String publisher){

}

setBook(String title, String publisher,float price){

}



}

Example: Overriding


class BookDetails{

            String title;


setBook(String title){ }


}

class ScienceBook extends BookDetails{


            setBook(String title){}                                             //overriding


setBook(String title, String publisher,float price){ }  //overloading


}


4.What is method overloading?

Method Overloading means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.

Note:

Overloaded methods MUST change the argument list

Overloaded methods CAN change the return type

Overloaded methods CAN change the access modifier

Overloaded methods CAN declare new or broader checked exceptions

A method can be overloaded in the same class or in a subclass


5.What is method overriding?

Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.

Note:

The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can’t override a method marked public and make it protected).

You cannot override a method marked final

You cannot override a method marked static

 

6) What is inheritance?

 Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

public class Parent{

public String parentName;

public int parentage;

public String familyName;

}

public class Child extends Parent{

public String childName;

public int childAge;

public void printMyName(){

System.out.println(“ My name is “+ chidName+” “ +familyName)

}

}


In above example the child has inherit its family name from the parent class just by inheriting the class.


7) What is multiple inheritance and does java support?

 If a child class inherits the property from multiple classes is known as multiple inheritance. 

Java does not allow to extend multiple classes but to overcome this problem it allows to implement multiple Interfaces.


8).What is Abstraction?

Abstraction refers to the act of representing essential features without including the background details or explanations.

public class Vehicle {


public String colour;

public String model;

}


9) What is encapsulation?

Ans) The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how thinigs work and just exposing the requests a user can do.


10) What is Association?

Ans) Association is a relationship between two classes. In this relationship the object of one instance perform an action on behalf of the other class. The typical behaviour can be invoking the method of other class and using the member of the other class.


public class MyMainClass{

public void init(){

new OtherClass.init();

}

}

11) What is Aggregation?

Ans) Aggregation has a relationship between two classes. In this relationship the object of one class is a member of the other class. Aggregation always insists for a direction.


public class MyMainClass{

OtherClass otherClassObj = new OtherClass();

}

11) What is Composition?

Ans) Composition is a special type of aggregation relationship with a difference that its the compulsion for the OtherClass object (in previous example) to exist for the existence of MyMainClass.


12)How to invoke a superclass version of an Overridden method?

To invoke a superclass method that has been overridden in a subclass, you must either call the method directly through a superclass instance, or use the super prefix in the subclass itself. From the point of the view of the subclass, the super prefix provides an explicit reference to the superclass' implementation of the method.

 // From subclass

super.overriddenMethod();


13).What is super?

super is a keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword. In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword. 

Note:

You can only go back one level.

In the constructor, if you use super(), it must be the very first code, and you cannot access any this.xxx variables or methods to compute its parameters.


14.How do you prevent a method from being overridden?

To prevent a specific method from being overridden in a subclass, use the final modifier on the method declaration, which means "this is the final implementation of this method", the end of its inheritance hierarchy.

                       public final void exampleMethod() {

                          //  Method statements

                          }


15.What is an Interface?

An interface is a description of a set of methods that conforming implementing classes must have.

Note:

You can’t mark an interface as final.

Interface variables must be static.

An Interface cannot extend anything but another interfaces.

 16.Can we instantiate an interface?

You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.


17)What is a marker interface?

Marker interfaces are those which do not declare any required methods, but signify their compatibility with certain operations. The java.io.Serializable interface and Cloneable are typical marker interfaces. These do not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.

18.What is an abstract class?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. 

Note:

If even a single method is abstract, the whole class must be declared abstract.

Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

You can’t mark a class as both abstract and final.


19.Can we instantiate an abstract class?

An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).

20.What is Constructor?

A constructor is a special method whose task is to initialize the object of its class.

It is special because its name is the same as the class name.

They do not have return types, not even void and therefore they cannot return values.

They cannot be inherited, though a derived class can call the base class constructor.

Constructor is invoked whenever an object of its associated class is created.

21.How does the Java default constructor be provided?

If a class defined by the code does not have any constructor, compiler will automatically provide one no-parameter-constructor (default-constructor) for the class in the byte code. The access modifier (public/private/etc.) of the default constructor is the same as the class itself.


22.Can constructor be inherited?

No, constructor cannot be inherited, though a derived class can call the base class constructor.


23.What are the differences between Class Methods and Instance Methods?

Class Methods Instance Methods

Class methods are methods which are declared as static. The method can be called without creating an instance of the class Instance methods on the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword.

Instance methods operate on specific instances of classes.

Class methods can only operate on class members and not on instance members as class methods are unaware of instance members. Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class.

Class methods are methods which are declared as static. The method can be called without creating an  instance of the class.

24.What are Access Specifiers?

One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifiers..

25.What are Access Specifiers available in Java?

Java offers four access specifiers, listed below in decreasing accessibility:

Public- public classes, methods, and fields can be accessed from everywhere.

Protected- protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.

Default(no specifier)- If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.

Private- private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.

 Situation   public   protected   default   private 

 Accessible to class 

 from same package?  yes yes yes no

 Accessible to class 

 from different package?  yes  no, unless it is a subclass  no no

26.What is final modifier?

The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method.

final Classes- A final class cannot have subclasses.

final Variables- A final variable cannot be changed once it is initialized.

final Methods- A final method cannot be overridden by subclasses.


27.What are the uses of final method?

There are two reasons for marking a method as final:

Disallowing subclasses to change the meaning of the method.

Increasing efficiency by allowing the compiler to turn calls to the method into inline Java code.


28.What is static block?

Static block which exactly executed exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute.


29.What are static variables?

Variables that have only one copy per class are known as static variables. They are not attached to a particular instance of a class but rather belong to a class as a whole. They are declared by using the static keyword as a modifier.

  static type  varIdentifier;

where, the name of the variable is varIdentifier and its data type is specified by type.

Note: Static variables that are not explicitly initialized in the code are automatically initialized with a default value. The default value depends on the data type of the variables.


30.What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

 31.What are static methods?

Methods declared with the keyword static as modifier are called static methods or class methods. They are so called because they affect a class as a whole, not a particular instance of the class. Static methods are always invoked without reference to a particular instance of a class.

Note:The use of a static method suffers from the following restrictions:

A static method can only call other static methods.

A static method must only access static data.

A static method cannot reference to the current object using keywords super or this.


Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: