Thursday, June 23, 2016

OOPs Concepts

The four basic concepts of OOP are: 
a.) Abstraction 
b.) Polymorphism 
c.) Inheritance
d.) Encapsulation

What is friend function?

- Friend function is a friend of a class. 
- It is allowed to access Public, private or protected data of that class. 
- It can be declared anywhere in the class declaration
- It doesn’t have any effect of access control keywords like private, public or protected.


What is the function of pure virtual functions?

Pure virtual function in object oriented programming is called as virtual method that acts as a function allowing its behavior to be overridden by the class that is inheriting the pure virtual function with the same signature. This is used in case of polymorphism. It is used when a base class is being driven by the derived class and an object of the derived class referred as base or derived class type. When a derived class overrides the base class method then the output or the behavior will be called as ambiguous. To use the virtual function a virtual keyword is used. This allows the function to be defined in every derived class and use the functionality of it.

Can we overload a static method in Java? 
Yes, you can overload a static method in Java. You can declare as many static methods of the same name as you wish provided all of them have different method signatures. See the answer for more detailed explanation and code example.


Can we override static method in Java? 
No, you cannot override a static method because it's not bounded to an object. Instead, static methods belong to a class and resolved at compile time using the type of reference variable. But, Yes, you can declare the same static method in a subclass, that will result in method hiding i.e. if you use reference variable of type subclass then new method will be called, but if you use reference variable of superclass than old method will be called.


Can we prevent overriding a method without using the final modifier? (answer)
Yes, you can prevent the method overriding in Java without using the final modifier. In fact, there are several ways to accomplish it e.g. you can mark the method private or static, those cannot be overridden.


Can we override a private method in Java? 
No, you cannot. Since the private method is only accessible and visible inside the class they are declared, it's not possible to override them in subclasses. Though, you can override them inside the inner class as they are accessible there.


What is covariant method overriding in Java? 
In covariant method overriding, the overriding method can return the subclass of the object returned by original or overridden method. This concept was introduced in Java 1.5 (Tiger) version and it's very helpful in case original method is returning general type like Object class, because, then by using covariant method overriding you can return more suitable object and prevent client side type casting. One of the practical use of this concept is in when you override the clone() method in Java.


Read more: http://java67.blogspot.com/2015/12/top-30-oops-concept-interview-questions-answers-java.html#ixzz4CQOK9xam

1 comment:

  1. Civil Lab Equipment Manufacturer is the leading Manufacturer, Supplier and Exporter of Civil Engineering Lab Equipments or instruments. Established in 2005.

    Mob: +91-9891445495, +91-8448366515, +918587026175
    Phone : +91-11-23657121
    Website : http://setestindia.com, http://civillabequipmentmanufacturer.com/

    ReplyDelete