Tuesday 2 April 2024

Object Oriented Programming In Java Questions And Answers - 1

What is OOPs? 

  • OOPs stands for Object-Oriented Programming system. 
  • It is a programming paradigm in which software design involves around data, or objects, rather than functions and logic. 
  • In OOPs, objects are data fields that have unique attributes and properties. 
  • OOPs focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. 
  • That's why large, complex and actively updated or maintained program is well suited for OOPs paradigm of programming.

  • It is based on objects.
  • It is based on real world.
  • It provides data hiding so it is very secure.
  • It provides reusability feature.
  • Eg. C++, Java, python etc.


What are the basic features of OOPs? 

    • Encapsulation

    • Inheritance

    • Polymorphism

    • Abstraction

Why use OOPs?

OOPs provide the following advantages, that's why it is used.

    • Code maintenance

    • Reusability

    • Security

    • Better productivity

    • Polymorphism flexibility

    • Problem solving

    • Easy troubleshooting

    • Design benefits

    • Data redundancy 

What is a class? 

  • A class is a collection of objects. Classes don’t consume any space in the memory.
  • It is a user defined data type that act as a template for creating objects of the identical type. 
  • Once a class has been defined. A large number of objects can be created using the same class. 
  • Therefore, Class is considered as the blueprint for the object. 

eg. 

  • Fruit is a class and its objects are mango, apple etc. 
  • Furniture is a class and its objects are table, chair, desk etc. 

Syntax : 

class classname [extends inheritance]

{

[field declararion]

[method declaration]

What is an object? 

  • An object is a real world entity which have properties and functionalities.
  • Object is also called an instance of class. Objects take some space in memory.
  • For eg. car, table, chair etc. are the example of objects.

Syntax of creation of object :

classname obj_reference = new classname(); 

Step I - classname obj_reference; // object declararion

Step II - obj_reference = new classname(); //object initialization

What is the difference between a class and an object? 

Class :

  •  It is a collection of objects.
  •  It doesn't take up space in memory.
  •  Class does not exist physically
  •  Classes are declared just once
  •  Eg. - Fruit, Vehicle, Laptop

Object

  •  It is an instance of a class.
  •  It takes space in memory.
  •  Object exist physically.
  •  Objects can be declared as and when required
  •  Eg. - Mango, Car, HP Laptop

What is encapsulation?

  • Encapsulation is an striking feature of OOPs that provides data security. 
  • It is a mechanism of binding member data and member function together into a single place i.e. class.
  • The main advantage of encapsulation is that data is hidden and protected from access by outside non-member methods of a class. 
  • In other words, only member functions defined in a class will have access to the data.
  • In encapsulation, data(variables) are declared as private and methods are declared as public. 

What are access specifiers? 

  • Access specifiers are the most important part of object oriented programming paradigm. 
  • It allows us to restrict the scope or visibility of a package, class, constructor, methods, variables, or other data members.
  • By using access specifiers, we define how the members (attributes and methods) of a class can be accessed. 

There are three types of most common access specifiers, which are following.

• Private

• Public

• Protected 

What is the difference between public, private and protected access modifiers?

Public Modifiers - When we declare any class, variable or method with public modifiers that means that class, variable or method is accessible throughout from within or outside the class, within or outside the package, etc.

It provides highest level of accessibility.

Private Modifiers - When we declare any class, variable or method with private modifiers that means that class, variable or method is not accessible from within or outside the class, within or outside the package, etc.

Private field or method can't be inherited to sub class. 

This provides lowest level of accessibility. 

Protected Modifiers - When we declare any class, variable or method with protected modifiers that means that class, variable or method is accessible from classes in

the same package, sub-classes in the same package, subclasses in other packages but not accessible from classes in other packages. 

What is data abstraction?

  • Data abstraction is an important feature of OOPs that allows to hide unnecessary data from the user. 
  • This reduces program complexity efforts.
  • This means that it displays only the necessary information to the user and hides all the internal background details.
  • For example, when we order anything in a restaurant, we get our orders but we don't know all the background work that has been taken to process this order.
  • If we talk about data abstraction in programming language, the code implementation is hidden from the user and only the necessary functionality is shown or provided to the user. 

How to achieve data abstraction? 

We can achieve data abstraction by using -

1. Abstract class

2. Interface 

What is an abstract class? 

  • Abstract class is that class which contains abstract method. 
  • Abstract methods are those methods which have only declaration not the implementation.
  • An abstract class is declared with abstract keyword. 
  • An abstract class can also contain non-abstract methods.

What is an interface? 

  • An interface is a collection of abstract method (methods without definition). 
  • We can't instantiate an interface.
  • Interface only contains the final fields and abstract methods not the regular methods. 
  • Interface also don't contain constructor. 
  • The member of the class may or may not be private but the members of the interface can never be private.
  • The abstract method of the interface must be overridden in its child classes and the interface supports the concept of multiple inheritance which was not supported using the classes.
  • An interface can't be extended by class, it can only be implemented by class. 

Syntax : 

interface interfacename

{

 [final field declaration]

 [abstract method declaration]

Differentiate between data abstraction and encapsulation.

Data Abstraction:

    1. In data abstraction, we hide unnecessary information from the user and only show the necessary information.

    2. Abstraction is implemented using abstract class and interfaces.

    3. Data abstraction is implemented at the design or interface level

Encapsulation

    1. In encapsulation, we combine data and functions together at a single unit i.e. class.

    2. Encapsulation is implemented using access modifiers such as rivate, public and protected.

    3. Encapsulation is implemented at the implementation level.

Can we create an instance of an abstract class? 

No,  instance of an abstract class can't be created.

No comments:

Salesforce AI Associate Certification - 3

What is semantic retrieval in the context of LLMs?   Searching for relevant information in other data sources What additional protection do...