OOP
10. What is an object?
An object refers to the instance of the class, which contains the instance of the members and behaviors defined in the class template. In the real world, an object is an actual entity to which a user interacts, whereas class is just the blueprint for that object. So the objects consume space and have some characteristic behavior.For example, a specific car.
17. How much memory does a class occupy?
Classes do not consume any memory. They are just a blueprint based on which objects are created. Now when objects are created, they actually initialize the class members and methods and therefore consume memory.
33. How is data abstraction accomplished?
Data abstraction is accomplished with the help of abstract methods or abstract classes.
26. What are the various types of inheritance?
- Single inheritance: where a subclass extends a single superclass. - Multiple inheritance: where a subclass extends multiple superclasses. However, Java does not support multiple inheritance of classes, but it can be achieved using interfaces. - Multilevel inheritance: where a subclass extends a class which in turn extends another class. - Hierarchical inheritance: where multiple subclasses extend a single superclass. - Hybrid inheritance: which is a combination of multiple and hierarchical inheritance.
25. Are there any limitations of Inheritance?
- The base class and the child class, are very tightly coupled together. So if one needs to make some changes, they might need to do nested changes in both classes, making it difficult to modify or replace the parent class without affecting the child classes. - Inheritance cannot be used to inherit from multiple classes at the same time, which can limit the flexibility of the class hierarchy.
9. What is a class?
A class can be understood as a template or a blueprint, which contains some values, known as member data or member, and some set of rules, known as behaviors or functions. So when an object is created, it automatically takes the data and functions that are defined in the class.Therefore the class is basically a template or blueprint for objects. Also one can create as many objects as they want based on a class. For example, first, a car's template is created. Then multiple units of car are created based on that template.
16. What is Abstraction?
Abstraction provides a way to model real-world entities by defining a set of abstract characteristics or properties that define the entity's behavior, without specifying how these characteristics are implemented. So abstraction allows hiding the implementation details of a class and showing only the necessary features to the user. In Java, abstraction is achieved using abstract classes and interfaces, which define a set of methods that must be implemented by any class that inherits from them, without providing the implementation details of those methods. This allows the user to interact with the abstract class or interface without knowing the implementation details of its methods.
36. What are access specifiers and what is their significance?
Access specifiers, as the name suggests, are a special type of keywords, which are used to control or specify the accessibility of entities like classes, methods, etc. Some of the access specifiers or access modifiers include "private", "public", etc. These access specifiers also play a very vital role in achieving Encapsulation - one of the major features of OOPs.
34. What is an abstract class?
An abstract class is a special class containing abstract methods. The significance of abstract class is that the abstract methods inside it are not implemented and only declared. So as a result, when a subclass inherits the abstract class and needs to use its abstract methods, they need to define and implement them.
37. What is an exception?
An exception can be considered as a special event, which is raised during the execution of a program at runtime, that brings the execution to a halt. The reason for the exception is mainly due to a position in the program, where the user wants to do something for which the program is not specified, like undesirable input.
29. What is an interface?
An interface refers to a special type of class, which contains methods, but not their definition. Only the declaration of methods is allowed inside an interface. To use an interface, you cannot create objects. Instead, you need to implement that interface and define the methods for their implementation.
13. What is Compile time Polymorphism and how is it different from Runtime Polymorphism?
Compile time Polymorphism is resolved during the compilation of the program. It is also known as static or method overloading. Method overloading is a feature that allows a class to have multiple methods with the same name, but different parameters. Runtime Polymorphism is resolved at runtime. It is also known as dynamic binding or method overriding. Method overriding is a feature that allows a subclass to provide its own implementation of a method that is already provided by its parent class. The key difference is the timing of resolution of which method to call. In Compile time Polymorphism, the method to be called is determined at compile time based on the method signature and the parameters passed. In Runtime Polymorphism, the method to be called is determined at execution time based on the actual type of the object that the method is being called on.
19. What is a constructor?
Constructors are special methods whose name is the same as the class name. The constructors serve the special purpose of initializing the objects.For example, suppose there is a class with the name "MyClass", then when you instantiate this class, you pass the syntax: MyClass myClassObject = new MyClass(); Now here, the method called after "new" keyword - MyClass(), is the constructor of this class. This will help to instantiate the member data and methods and assign them to the object myClassObject.
31. What is meant by dynamic polymorphism?
Dynamic Polymorphism or Runtime polymorphism refers to the type of Polymorphism in OOPs, by which the actual implementation of the function is decided during the runtime or execution. The dynamic or runtime polymorphism can be achieved with the help of method overriding.
11. What is encapsulation?
Encapsulation is the mechanism of hiding the internal state of an object and providing access to it only through publicly visible methods. In Java, encapsulation is implemented using access modifiers (public, private, and protected) to restrict access to the fields and methods of a class. 1) Data hiding: Encapsulation is the process of hiding unwanted information, such as restricting access to any member of an object. 2) Data binding: Encapsulation is the process of binding the data members and the methods together as a whole, as a class.
15. What is meant by Inheritance?
Inheritance is the mechanism by which an class is created using the definition of another class. The class that is being inherited from is called the superclass, while the class that is inheriting is called the subclass. It allows the subclass to inherit properties and methods from the superclass, which helps to reduce code duplication and increases code consistency. In addition, inheritance allows subclasses to add or modify the behavior of the superclass by overriding its methods or adding new methods and properties. Inheritance not only helps to keep the implementation simpler but also helps to facilitate code reuse.
35. How is an abstract class different from an interface?
Interface and abstract class both are special types of classes that contain only the methods declaration and not their implementation. But the interface is entirely different from an abstract class. The main difference between the two is that, when an interface is implemented, the subclass must define all its methods and provide its implementation. Whereas when an abstract class is inherited, the subclass does not need to provide the definition of its abstract method, until and unless the subclass is using it. Also, an abstract class can contain abstract methods as well as non-abstract methods.
38. What is meant by exception handling?
No one wants its software to fail or crash. Exceptions are the major reason for software failure. The exceptions can be handled in the program beforehand and prevent the execution from stopping. This is known as exception handling.So exception handling is the mechanism for identifying the undesirable states that the program can reach and specifying the desirable outcomes of such states.Try-catch is the most common method used for handling exceptions in the program.
18. Is it always necessary to create objects from class?
No. An object is necessary to be created if the base class has non-static methods. But if the class has static methods, then objects don't need to be created. You can call the class method directly in this case, using the class name.
40. Can we run a Java application without implementing the OOPs concept?
No. Java applications are based on Object-oriented programming models or OOPs concept, and hence they cannot be implemented without it. However, on the other hand, C++ can be implemented without OOPs, as it also supports the C-like structural programming model.
7. What are some advantages of using OOPs?
OOPs is very helpful in solving very complex level of problems. Highly complex programs can be created, handled, and maintained easily using object-oriented programming. OOPs, promote code reuse, thereby reducing redundancy. OOPs also helps to hide the unnecessary details with the help of Data Abstraction. OOPs, are based on a bottom-up approach, unlike the Structural programming paradigm, which uses a top-down approach. Polymorphism offers a lot of flexibility in OOPs.
6. What are the main features of OOPs?
OOPs or Object Oriented Programming mainly comprises of the below four features, and make sure you don't miss any of these: - Inheritance - Encapsulation - Polymorphism - Data Abstraction
8. Why is OOPs so popular?
OOPs programming paradigm is considered as a better style of programming. Not only it helps in writing a complex piece of code easily, but it also allows users to handle and maintain them easily as well. Not only that, the main pillar of OOPs - Data Abstraction, Encapsulation, Inheritance, and Polymorphism, makes it easy for programmers to solve complex scenarios. As a result of these, OOPs is so popular.
1. What is meant by the term OOPs?
OOPs refers to Object-Oriented Programming. It is the programming paradigm that is defined using objects. Objects can be considered as real-world instances of entities like class, that have some characteristics and behaviors.
39. What is meant by Garbage Collection in OOPs world?
Object-oriented programming revolves around entities like objects. Each object consumes memory and there can be multiple objects of a class. So if these objects and their memories are not handled properly, then it might lead to certain memory-related errors and the system might fail. Garbage collection refers to this mechanism of handling the memory in the program. Through garbage collection, the unwanted memory is freed up by removing the objects that are no longer needed.
32. What is the difference between overloading and overriding?
Overloading is a compile-time polymorphism feature in which an entity has multiple implementations with the same name. For example, Method overloading and Operator overloading. Whereas Overriding is a runtime polymorphism feature in which an entity has the same name, but its implementation changes during execution. For example, Method overriding.
12. What is Polymorphism?
Polymorphism refers to the process by which some code, data, method, or object behaves differently under different circumstances or contexts. Polymorphism is a feature that allows objects of different classes to be treated as if they were objects of a single superclass. This means that objects of different classes can be used interchangeably, as long as they inherit from the same superclass or implement the same interface. Polymorphism allows for more flexible and reusable code, as well as the ability to write code that can work with a variety of different objects without needing to know their specific types..
30. What is meant by static polymorphism?
Static Polymorphism is commonly known as the Compile time polymorphism. Static polymorphism is the feature by which an object is linked with the respective function or operator based on the values during the compile time. Static or Compile time Polymorphism can be achieved through Method overloading or operator overloading.
28. Define a superclass?
Superclass is also a part of Inheritance. The superclass is an entity, which allows subclasses or child classes to inherit from itself.
27. What is a subclass?
The subclass is a part of Inheritance. The subclass is an entity, which inherits from another class. It is also known as the child class.
2. What is the need for OOPs?
There are many reasons why OOPs is mostly preferred, but the most important among them are: 1. OOPs helps users to understand the software easily, although they don't know the actual implementation. 2. With OOPs, the readability, understandability, and maintainability of the code increase multifold. 3. Even very big software can be easily written and managed easily using OOPs.