Question:- What are the rules for creating a constructor?
Answer:- • It cannot have a return type. • It must have the same name as the Class name. • It cannot be marked as static. • It cannot be marked as abstract. • It cannot be overridden. • It cannot be final.
Question:- What are the characteristics of an abstract class?
Answer:- An abstract class is a class that is declared as abstract. It cannot be instantiated and is always used as a base class. The characteristics of an abstract class are as follows: • Instantiation of an abstract class is not allowed. It must be inherited. • An abstract class can have both abstract and non-abstract methods. • An abstract class must have at least one abstract method. • You must declare at least one abstract method in the abstract class. • It is always public. • It is declared using the abstract The purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share.
Question:- Is it possible for a class to inherit the constructor of its base class?
Answer:- No, a class cannot inherit the constructor of its base class.
Question:- Identify which OOPs concept should be used in the following scenario?
Answer:- A group of 5 friends, one boy never gives any contribution when the group goes for the outing. Suddenly a beautiful girl joins the same group. The boy who never contributes is now spending a lot of money for the group. Runtime Polymorphism
Question:- What is composition?
Answer:- Composition is one of the vital concepts in OOP. It describes a class that references one or more objects of other classes in instance variables. It allows us to model a has-a association between objects. We can find such relationships in the real world. For example, a car has an engine. the following figure depicts the same
Question:- What are the differences between copy constructor and assignment operator?
Answer:- The copy constructor and the assignment operator (=) both are used to initialize one object using another object. The main difference between the two is that the copy constructor allocates separate memory to both objects i.e. existing object and newly created object while the assignment operator does not allocate new memory for the newly created object. It uses the reference variable that points to the previous memory block (where an old object is located).
Question:- What is the difference between Composition and Inheritance?
Answer:- Inheritance means an object inheriting reusable properties of the base class. Compositions mean that an object holds other objects. In Inheritance, there is only one object in memory (derived object) whereas, in Composition, the parent object holds references of all composed objects. From a design perspective, inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.
Question:- What is constructor chaining?
Answer:- In OOPs, constructor chaining is a sequence of invoking constructors (of the same class) upon initializing an object. It is used when we want to invoke a number of constructors, one after another by using only an instance. In other words, if a class has more than one constructor (overloaded) and one of them tries to invoke another constructor, this process is known as constructor chaining. In C++, it is known as constructor delegation and it is present from C++ 11.
Question:- What are the limitations of inheritance?
Answer:- • The main disadvantage of using inheritance is two classes get tightly coupled. That means one cannot be used independently of the other. If a method or aggregate is deleted in the Super Class, we have to refactor using that method in SubClass. • Inherited functions work slower compared to normal functions. • Need careful implementation otherwise leads to improper solutions.
Question:- What is Coupling in OOP and why it is helpful?
Answer:- In programming, separation of concerns is known as coupling. It means that an object cannot directly change or modify the state or behavior of other objects. It defines how closely two objects are connected together. There are two types of coupling, loose coupling, and tight coupling. Objects that are independent of one another and do not directly modify the state of other objects is called loosely coupled. Loose coupling makes the code more flexible, changeable, and easier to work with. Objects that depend on other objects and can modify the states of other objects are called tightly coupled. It creates conditions where modifying the code of one object also requires changing the code of other objects. The reuse of code is difficult in tight coupling because we cannot separate the code. Since using loose coupling is always a good habit.
Question:- Name the operators that cannot be overload.
Answer:- 1. Scope Resolution Operator (::) 2. Ternary Operator (? :) 3. Member Access or Dot Operator (.) 4. Pointer to Member Operator (.*) 5. sizeof operator
Question:- What is the difference between new and override?
Answer:- The new modifier instructs the compiler to use the new implementation instead of the base class function. Whereas, Override modifier helps to override the base class function. • virtual: indicates that a method may be overridden by an inheritor • override: Overrides the functionality of a virtual method in a base class, providing different functionality. • new: Hides the original method (which doesnt have to be virtual), providing different functionality. This should only be used where it is absolutely necessary. When you hide a method, you can still access the original method by upcasting to the base class. This is useful in some scenarios, but dangerous.
Question:- Explain overloading and overriding with example?
Answer:- • Overloading Overloading is a concept in OOP when two or more methods in a class with the same name but the method signature is different. It is also known as compile-time polymorphism. For example, in the following code snippet, the method add() is an overloaded method. • Overriding If a method with the same method signature is presented in both child and parent class is known as method overriding. The methods must have the same number of parameters and the same type of parameter. It overrides the value of the parent class method. It is also known as runtime polymorphism. For example, consider the following program.
