Ans: There are four basic principles that make a language Object Oriented. They are as follows:
Encapsulation: It is the mechanism of hiding data implementation by restricting access to public methods.
Abstraction: It is a technique by which we can hide the data that is not required to a user.
Inheritance: It is a process by which a child class acquires all the properties and behaviors of the parent class.
Polymorphism: Polymorphism means many forms. It is the technique by which we can perform a single task in different ways. It is further divided into two types – static and dynamic.
Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding.
Ans: Object-oriented programming (OOP) in Java is a programming methodology or paradigm (model) to design a computer program using classes and objects.
It is the most popular programming paradigm and widely used in the software industry today. It is an extension of procedural programming.
Ans: Procedural programming means writing code without objects.
Object-oriented programming means writing code with objects which contain data in the form of fields (often known as properties in java) and functionality code in the form of methods (often known as behavior in java).
Ans: Writing a program using features like encapsulation, abstraction, inheritance, and polymorphism is called object-oriented programming system.
C#, C++ is a procedural language with object-oriented extension.
Ans: Object Oriented Programming languages like Java, Python, C++ follow concepts of OOPS like Encapsulation, Abstraction, Polymorphism, and Inheritance, etc.
Object-Based Programming languages like JavaScript, VBScript, etc follow some features of OOPS but they do not support Polymorphism and Inheritance.
Ans: Some of the important striking features of OOPs concept are as follows:
Higher priority is focused on data rather than functions.
Programs are divided into the number of entities known as objects.
Objects communicate with each other through functions (methods).
Methods that operate on data of an object are closely bound together in the data structure.
Data is hidden in the OOP and cannot be accessed by external methods. Hence, it is safe from accidental modification.
Data cannot move freely out of the object.
New data and methods can be easily added whenever needs.
Ans: OOPs concept in Java offers several advantages that are not available in procedural programming like C, Pascal, etc. Some of the major and important benefits of object-oriented programming are as follows:
Security
Reusability
Effective communication
Developing complex software
Easily upgraded
Easy partition of work
Maintenance
Efficiency
Ans: Due to its reusability feature, it is widely used in many areas. Some of the important application areas of OOPs are as follows:
Real-time systems
Object-oriented database
Graphical user interface design in the Windows operating system.
Artificial intelligence and expert systems
Parallel programming
CAD/CAM software and in many areas.
Ans: There are several key points for developing a clean object-oriented code that must be kept in mind. They are:
Program to an interface (or the super type) not the implementation.
Interacting of Classes should be loosely coupled among themselves.
Code should implement tightly encapsulation. Avoid the use of public, static variables, and singleton design pattern wherever possible.
Always reuse the code using inheritance, composition, and utility methods.
Has-A relationship is better than Is-A relationship because it provides more flexibility.
In the case of multi-threading applications, use immutable objects to represent the state.
Make proper use of design patterns wherever possible.
Use up-to-date software dependencies and make the best use of the latest technology available to us.
Ans: Encapsulation is the programming technique of making fields in a class private and providing access to those fields via public methods.
In other words, encapsulation is the technique of hiding implementation details and providing methods for data access.
Ans: Data hiding is the process in which the field is declared private within the class to hide so that no one can access it from outside the class.
Ans: There are two key points thereby we can achieve or implement encapsulation in Java program.
Declaring the instance variable of the class as private so that it cannot be accessed directly by anyone from outside the class.
Provide the public setter and getter methods in the class to set/modify the value of the variable.
Ans: By declaring data members (variables) as private, we can achieve data hiding. If variables are declared as private in the class, nobody can access them from outside the class.
The biggest advantage of data hiding in java is we can achieve security in the application.
public final class Test {
private double radius = 1;
public double getArea() {
return radius * radius * Math.PI;
}
public static void main(String[] args)
{
Test t = new Test();
System.out.println("Radius of circle: " + t.radius);
System.out.println("Area of circle: " +t.getArea());
}
}Ans: Yes, the above code will be executed successfully. It will not cause any problem. The output is
Radius of circle: 1.0
Area of circle: 3.141592653589793Ans: If each variable is declared as private within the class, it is called tightly encapsulated class in Java.
class A
{
private int x = 20;
}
class B extends A
{
int y = 50;
}
class C extends A
{
private int z = 10;
}Ans: Class A and Class C are tightly encapsulated classes. Class B is not tightly encapsulated class because of the non-private variable y. Anyone can access it directly from outside the class.
class P {
int a = 10;
}
class Q extends P {
private int b = 20;
}
class R extends Q {
private int z = 30;
}Ans: None of these is a tightly encapsulated class because class Q is the child class of P. Non-private data member of class P by default is available in subclass Q.
Similarly, class R is the subclass of Q. The non-private data member of class Q by default is available inside the class R.
Ans: A method that is used to retrieve the value of a variable is called getter or accessor method.
A method that is used for updating or setting the value of a variable is called setter method or mutator method.
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String studentName) {
name = studentName;
}
}
class EncapsulatedTest {
public static void main(String[] arg) {
Student obj = new Student();
obj.name = "John";
}
}Ans: The above code will generate compile-time error because variable name has been declared as private in the class Student. So, the data member from outside the class cannot be accessed directly.
Ans: If we don’t use encapsulation in a program, fields will not be private and could be accessed by anyone from outside the class.
For more encapsulation interview questions, go to this tutorial: Java Encapsulation Interview Questions and Answers.
Ans: The technique of creating a new class by using an existing class functionality is called inheritance in Java. In other words, inheritance is a process where a child class acquires all the properties and behaviors of the parent class.
Ans: Inheritance is one of main pillars of OOPs concept. Some objects share certain properties and behaviors. By using inheritance, a child class acquires all properties and behaviors of parent class.
There are the following reasons to use inheritance in java.
We can reuse the code from the base class.
Using inheritance, we can increase features of class or method by overriding.
Inheritance is used to use the existing features of class.
It is used to achieve runtime polymorphism i.e method overriding.
Ans: Is-A relationship represents Inheritance. It is implemented using the “extends” keyword. It is used for code reusability.
Ans: A class from where a subclass inherits features is called superclass. It is also called base class or parent class.
A class that inherits all the members (fields, method, and nested classes) from another class is called subclass. It is also called a derived class, child class, or extended class.
Ans: Inheritance can be implemented or achieved by using two keywords:
extends: extends is a keyword that is used for developing the inheritance between two classes and two interfaces.
implements: implements keyword is used for developing the inheritance between a class and interface.
For more Inheritance Interview Questions for best practice, go to this tutorial: Top 50 Java Inheritance Interview Questions and Answers for Freshers and experienced
Ans: No. If School is a superclass and Student is the subclass then the following reference would be wrong – Student st = new School();
Ans: Yes. It is applicable in java. An example is – School sc = new Student();
Ans: super keyword is a reference variable that refers to an immediate superclass object. It is used for the following purposes:
To refer immediate parent class instance variable.
To call immediate parent class constructor.
To invoke immediate superclass method.
Ans: this keyword is a reference variable that refers to the current class object. It holds the reference to current class object or same class object.
There are six usages of Java this keyword. They are as follows:
this reference can be used to refer to the current class instance variable.
this keyword is used to call the non-static method of the current class.
this() can be used to invoke the current class constructor.
this keyword can be used as a parameter in the method call.
The keyword “this” can be used as a parameter in the constructor call.
It can also be used to return the object of the current class from the method.
Is it possible to use this() and super() both in the same constructor?
Ans: No, Java does not allow using both super() and this() together in same constructor. As per Java specification, super() or this() must be the first statement in a constructor.