Tuesday, 2 April 2024

Object Oriented Programming In Java Questions And Answers - 3

 What is polymorphism?

  • Polymorphism is one of the major pillars of OOPs. 
  • It is a multiple form of a single entity.
  • It is process of performing a single task in different ways.
  • In polymorphism, one method have multiple forms based on the type of parameters, order of parameters, and number of parameters.
  • In simple words, Polymorphism is the ability of an object to take many forms. 

What are the types of polymorphism?

There are two types of polymorphism -

• Compile time polymorphism

• Run time polymorphism

What is compile time polymorphism?

  • The polymorphism that takes place during compile time is called compile time polymorphism. 
  • It is also called static polymorphism or early binding.
  • In this type of polymorphism, a class contain multiple methods having same name but different signatures.
  • Example of CTP - Method Overloading, Operator Overloading. 

What is runtime polymorphism? 

  • The polymorphism that takes place during run time is called compile time polymorphism. 
  • It is also called dynamic polymorphism or late binding. 
  • Runtime polymorphism refers to the process when a call to an overridden process is resolved at the run time.
  • In this type of polymorphism, the sub class and base class both contain methods having same name which can have different functionalities.

What is method overloading?

  • When a class contains multiple methods having same name but different signature, this is called method overloading. 
  • In method overloading, multiple methods of same names performs different tasks within the same class.
  • It depends upon the number and type of argument in the argument list and doesn't depend upon the return type of the method.
  • This is an example of compile time polymorphism. 
  • What is method overriding? 
  • When base class contain a method and sub class also contain same name method of its parent class, this is called method overriding. 
  • In other words, base class and subclass both have same name methods as well as signatures too. 
  • Method overriding is an example of run time polymorphism. 
  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. 

What is operator overloading?

  • Operator overloading is a mechanism in which the operator is overloaded to provide the special meaning to the userdefined data type.
  • It is an example of compile time polymorphism. 

What is static function? 

  • Static functions are those functions that can be called without creating an object of the class. 
  • That means, Static methods do not use any instance variables of any object of the class they are defined in. 
  • Static methods can not be overridden. They are stored in heap space of the memory. 

What are virtual functions? 

  • Virtual function is a function or method used to override the behavior of the function in an inherited class with the same signature to achieve the polymorphism. Virtual function defined in the base class and overridden in the inherited class.
  • The Virtual function cannot be private, as the private functions cannot be overridden. 
  • It is used to achieve runtime polymorphism. 

 What are pure virtual functions? 

  •  A pure virtual function is that function which have no definition. 
  •  That means a virtual function that doesn't need implementation is called pure virtual function.
  •  A pure virtual function have not definitions but we must override that function in the derived class, otherwise the derived class will also become abstract class. 

 What is Constructor? 

  •  Constructor is a special type of member function which is used to initialize an object. 
  •  It is similar as functions but it's name should be same as its class name and must have no explicit return type.
  •  It is called when an object of the class is created. 
  •  At the time of calling constructor, memory for the object is allocated in the memory.
  •  We use constructor to assign values to the class variables at the time of object creation. 

 What are the types of Constructor?

 Constructor have following types -

  • Default constructor

  • Parameterized constructor

  • Copy constructor

  • Static constructor

• Private constructor

What is default constructor? 

A constructor with 0 parameters is known as default constructor. 

What is parameterized constructor?

The constructor method having the argument list or parameter list is called as parameterized constructor as it initializes the fields with the values of the parameters. 

What is copy constructor? 

A copy constructor is that constructor which use existing object to create a new object. It copy variables from another object of the same class to create a new object. 

What is static constructor? 

  • A static constructor is automatically called when the first instance is generated, or any static member is referenced.
  • The static constructor is explicitly declared by using a static keyword. 
  • However, the static constructor is not supported in Java. 

What is private constructor? 

  • Java enables us to declare a constructor as private. 
  • We can declare a constructor private by using the private access specifier. 
  • Note that if a constructor is declared private, we cannot create an object of the class.
  • Instead, we can use this private constructor in Singleton Design Pattern. 

What is destructor?

  • Destructor is a type of member function which is used to destroy an object. 
  • It is called automatically when the object goes out of scope or is explicitly destroyed by a call to delete.
  • It destroy the objects when they are no longer in use. 
  • A destructor has the same name as the class, preceded by a tilde (~). 

What is Constructor Overloading? 

  • The constructor method of the class may or may not have the argument list, and it has no return type specified and
  • we also know that method overloading depends only on the argument list and not on the return type. 
  • Thus, we can say that the constructor method can be overloaded. 

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...