Oops Question And Answers

1. What is OOPs?

Programmers can use objects to represent real-world circumstances thanks to object-oriented programming. Any entity with states and behaviors is an object. While methods define an item’s behaviors, states reflect the characteristics or data of an entity. Objects include students, workers, books, etc. By exchanging messages, these things communicate with one another. A class is also a template for building an object. A class is required in order to generate objects. For instance, there needs to be an Employee class in order to generate an Employee object.

2. Difference between Procedural programming and OOPs?

Procedural Programming Oops
Procedural Programming is based on functions. Object-oriented programming is based on real-world objects.
It shows the data to the entire program. It encapsulates the data.
It does not have a scope for code reuse. It provides more scope for code reuse.
It follows the concept of top-down programming. It follows a bottom-up programming paradigm.
The nature of the language is complicated. It is less complicated in nature, so it is easier to modify, extend and maintain.
It is hard to modify, extend and maintain the code.  

3. Why use OOPs?

Programming with OOP lets you package together data states and functionality to change those data states while keeping the specifics secret (Consider the analogy of a car, you can only see the steering of the car while driving, the circuitry behind it is hidden from you). As a result, OOP design produces flexible, modular, and abstract code. Because of this, it is very helpful for developing larger programs. Using classes and objects, you may include OOP into your code. The objects you construct will have the states and capabilities of the class to which they belong.

4. What are the basic concepts of OOPs?

The basic concepts of OOPs are:

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

5. What is Encapsulation?

Encapsulation is also a part of the OOPs concept. It refers to the bundling of data with the methods that operate on that data. It also helps to restrict any direct access to some of an object’s components.

6. What is Abstraction?

Abstraction is an OOPs concept to build the structure of real-world objects. It “shows” only essential attributes and “hides” unnecessary information from the outside. The main focus of abstraction is to hide unnecessary details from the users. It is one of the most important concepts of OOPs.

7. What is method overloading?

There is a concept where two or more methods can have the same name. But they should have different parameters, different numbers of parameters, different types, or both. These methods are known as overloaded methods and this feature is called method overloading

8. What is method overriding?

Method overriding is a concept of object-oriented programming.

It is a language feature that allows a subclass or child class to provide a specific implementation of a method which is already provided by one of its superclasses or parent classes.

9. Types of Inheritance in OOPS

Different types of inheritances in OOps are as follows:

  • Single Inheritance
  • Multiple Inheritance
  • Multi-level Inheritance
  • Multi-path Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
types of inheritance

10. What are the main features of OOPs?

The main features of OOPs are given as follows:

  • In OOP, you combine the code into one unit so you can specify the parameters of each piece of data. This process of wrapping up data into a single unit is called encapsulation. 
  • By using classes, you can generalise your object types and make your application easier to use. This is termed as an abstraction.
  • The ability for a class to inherit characteristics and behaviours from another class allows for more code reuse.
  • Polymorphism allows for the creation of several objects from a single, adaptable class of code.

11. Is it possible to call the base class method without creating an instance?

Yes, we can possibly call the base class method without creating an instance in the following 3 cases:

  1. If the method is static
  2. Calling the inherited method inside a derived class
  3. Calling the method using the base keyword from the sub-classes

The most popular case is that of the static methods.

12. What are the limitations of OOPs?

Following are some of the common limitations of OOPs:

  • Size exceeds that of other programs.
  • It took a lot of work to make, and it runs more slowly than other programs.
  • It is inappropriate for certain types of issues.
  • It takes some getting used to.

13What are constructors?

The constructor has the same name as the class.
A constructor is also a special kind of method. It is used to initialize objects of the class.

14. Types of constructor

Types of constructors depend upon languages

  • Private Constructor
  • Default Constructor
  • Copy Constructor
  • Static Constructor
  • Parameterized Constructor
Types of constructor

15What is the difference between a class and a structure?

Class: Class is basically a User-defined blueprint from which objects are created. It consists of methods ( set of instructions) that are performed on the objects.

Structure: A structure is also a user-defined collection of variables. Structures are also different data types.

A user-defined class serves layout or blueprint from which objects can be built. In essence, a class is made up of fields known as attributes and methods known as member functions that define actions. A structure is a grouping of variables of various data kinds under one heading.

More Question And Answer Related to Oops.

What is inheritance?

Whenever one class is derived from another, it is referred to as inheritance. The child class will inherit all of the parent class’s public and protected properties and methods. Apart from the attributes and methods inherited from the parent class, it can also have its own additional set of features. The’ extends’ keyword is used to specify an inherited class.

If you derive a  class from another class that is known as inheritance. The child class will inherit all the public and protected properties and methods from the parent class. The child class can also have its own properties and methods. An inherited class is defined by using the extends keyword.

What is inheritance

 

What Is Hybrid Inheritance ,Hierarchical Inheritance And Limitations Of Inheritance?

Hybrid Inheritance:-

The type of inheritance formed by the combination of different types of inheritances like single, multiple inheritances, etc. is classified as hybrid inheritance.

 Hierarchical Inheritance:-

In the case of a hierarchical inheritance, multiple subclasses inherit from a parent class. Hierarchical inheritance is a type of inheritance in which multiple classes are descended from a single parent or base class. For example, the fruit class can have ‘apple’, ’mango’, ’banana’, ‘cherry’ etc. as its subclasses.

 Limitations Of Inheritance:-

It Increases the execution time and effort. It also requires jumping back and forth between different classes. The parent class and the child class are always tightly coupled. Afford modifications in the program would require changes for the parent and the child’s class. Inheritance requires careful implementation otherwise it would lead to incorrect results.

What Is a Superclass , Subclass?

 Superclass:– A superclass is a class from which a subclass or child class is derived. Base class and parent class are other names for a superclass. For example, if Student is a class derived from the Person class, then the Person class will be referred to as the superclass.

A superclass or base class is also a class that works as a parent to some other class/ classes.

For example, the Vehicle class is a superclass of class Bike.

Subclass:– A class that derives from another class is referred to as a subclass. A subclass inherits the properties of its ancestors or parent classes. For example, the class Bike is a subclass or a derivative of the Vehicle class.

What is Polymorphism and their types static and dynamic polymorphism?

Polymorphism is one of the most used and core concepts in OOP languages. It explains the concept of different classes can be used with the same interface. Each of these classes can have its own implementation of the interface. 

 static polymorphism:-

In OOP, static polymorphism determines which method to call at compile time. For the same trigger with static polymorphism, the object might respond differently. Function, constructor and operator overloading are examples of static polymorphism.

 dynamic polymorphism:-

Dynamic polymorphism is a method or process that handles a call to an overridden method during runtime rather than at compile time. It is also referred to as dynamic method dispatch or runtime polymorphism. Using method overriding, we can create dynamic polymorphism. An example of runtime polymorphism: is method overriding.

 Differentiate between overloading and overriding.

When two or more methods in the same class have the same name but different parameters, this is referred to as overloading. The technique of using the same method signature, i.e., name and parameters, in both the superclass and the child class is known as overriding.

Differentiate between overloading and overriding

 

 What is Coupling in OOP, and why is it helpful?

The degree of dependency between the components is called coupling.

Types of Coupling

A. Tight Coupling – If the dependency between components is high, these components are called tightly coupled.

Ex: –

Below three Classes are highly dependent on each other hence they are tightly coupled.

class P
{
static int a = Q.j;
}
 
class Q
{
static int j = R.method();
}
 
class R
{
public static int method(){
return 3;
}

B.  Loose Coupling – If the dependency between components is low, it is called loose coupling. Loose coupling is preferred because of the following reasons:-

  1. It increases the maintainability of code
  2. It provides reusability of code

What are the types of variables in OOP?

Variables are basic units to store data in RAM for Java programs.

Variables should be declared before using them in Java programming. Variable initialization can be static or dynamic. The syntax for variable declaration and static initialization is: –

Types of variables

  • Primitive Variables: It is used to represent primitive values like int, float, etc.
  • Reference Variables: It is used to refer to objects in Java.
  • Instance Variables: Variables whose value varied from object to object are instance variables. For every object, a separate copy of the instance variable is created. Instance variables are declared within the Class and outside any method/block/constructor
  • Static variables: For static Variables, a single copy of the variable is created, and that copy is shared between every Class object. The static variable is created during class loading and destroyed at class unloading.
  • Static variables can be accessed directly from the static and instance area. We are not required to perform initialization explicitly for static variables, and JVM will provide default values.
  • Local Variables: Variables declared inside a method or block or constructor are local variables. Hence the scope of local variables is the same as the block’s scope in which we declared that variable.