Technical Interview Questions OOPS | Viva Questions C++ | Part 2

11. What is method overriding?
Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its base class. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.
Note:
• The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can’t override a method marked public and make it protected).
• You cannot override a method marked final
• You cannot override a method marked static
12. What is an abstract class?
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation.
Note:
• If even a single method is abstract, the whole class must be declared abstract.
• Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
• You can’t mark a class as both abstract and final.
13. When should I use abstract classes
Use Abstract Class when…
• If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
• When you want to provide a generalized form of abstraction and leave the implementation task with the inheriting subclass.
• Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies.
14. What is Constructor?
• A constructor is a special method whose task is to initialize the object of its class.
• It is special because its name is the same as the class name.
• They do not have return types, not even void and therefore they cannot return values.
• They cannot be inherited, though a derived class can call the base class constructor.
• Constructor is invoked whenever an object of its associated class is created.
15. Can constructor be inherited?
No, constructor cannot be inherited, though a derived class can call the base class constructor.
16. What are Access Specifiers?

One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. You can control access to classes, methods, and fields via so-called access specifiers..
17. What are Access Specifiers available?

• Public- public classes, methods, and fields can be accessed from everywhere.
• Protected- protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
• Private- private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.
18. What are static variables?

Variables that have only one copy per class are known as static variables. They are not attached to a particular instance of a class but rather belong to a class as a whole. They are declared by using the static keyword as a modifier. static type varIdentifier; Where, the name of the variable is varIdentifier and its data type is specified by type.
Note: Static variables that are not explicitly initialized in the code are automatically initialized
with a default value. The default value depends on the data type of the variables.
19.What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
20. What are static methods?

Methods declared with the keyword static as modifier are called static methods or class methods. They are so called because they affect a class as a whole, not a particular instance of the class. Static methods are always invoked without reference to a particular instance of a class.
Note:The use of a static method suffers from the following restrictions:
• A static method can only call other static methods.
• A static method must only access static data.
• A static method cannot reference to the current object using keywords super or this.
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...
 
Posts RSSComments RSSBack to top
© 2013 Updated Tech News Results and Reviews