OOPS Concepts
OOPS Concepts
-Basic Ques:
1.What is Coupling and Types?
Answer: when two classes (or) objects collaborate and work with each other to complete predefined
task. Types – loose coupling and Tight coupling.
2.What is Association?
3. What are the differences between Object Oriented and Object-based programming language?
1. Object-Oriented programming language supports all the features of Oops including inheritance
and Polymorphism.
2. Oops language don’t have built-in objects.
3. Examples: java, C++, etc.
Object-based programming language –
1. Object based programming language supports the usage of Object and encapsulation.
2. Object based programming language have built-in objects, for example JavaScript has window
object.
3. Examples: JavaScript, VB, etc.
Answer: Advantages:
1. Oops language allows you to break the program into the small problems that can be easily
solved.
2. Code reusability.
3. Easy to maintain.
4. Flexibility and scalability.
Disadvantages:
1. Increased complexity.
2. Overuse of inheritance.
3. Steeper learning curve which means it is difficult to learn for beginners.
Answer: An entity that has state and behavior is called as Object. It is a real-world entity. For examples,
car, chair, pen, table, etc...
6. What is class in java?
Answer: A class is template (or) blueprint form which objects are created. And class is group of objects.
7. What is method?
Answer: A method is a block of code or collection of statements are grouped together to perform a
certain task or operation.
Answer: A method does not have method body is called as abstract method. It is always declared inside
the abstract class only. If you declare a method as abstract, we can use abstract keyword.
Answer: A constructor is a special type of method which is used to initialize the objects.
Answer: A class having more than one constructor with different parameter list, it is known as
constructor overloading.
Answer: constructor:
Method:
Answer: java main () method is static. Because of the Object is not required to call a static method.
Answer: no, we cannot execute a program without main () method. Because when we run a program,
the JVM looks for the main method. If JVM could not find main method, it will show you run-time error
exception.
Answer: this keyword in java is a reference variable that refers to the current object.
Class:
-Inheritance:
1. what is inheritance?
Answer: Inheritance in java is a mechanism in which one object acquires all the properties and behavior
of a parent object. Also known as parent-child relationship.
Answer: The newly formed class which will created by taking the properties of an existing class/parent
class, it is known as sub class/child class. Also called as derived class.
Answer: A Super class is the class from where a subclass is inherits the features. Also called as base class.
Answer: There are mainly three types of inheritance which are supported by java – 1) single inheritance,
2) multi-level inheritance, 3) hierarchical inheritance.
Answer: when one class is derived from a single parent class is called as single inheritance.
Answer: When there is a chain of inheritance i.e., one class is derived from another class which is again
derived from another parent class, such type of inheritance is called multi-level inheritance.
8. What is hierarchical inheritance?
Answer: When one or more classes is derived from the single parent class. Such type of inheritance is
called as hierarchical inheritance.
Answer: When one class is derived from the two or more parent classes. Such type of inheritance is
called as multiple inheritance. Multiple inheritance is not supported by java.
-Polymorphism:
11. What is method overloading?
Answer: if a class having multiple methods with same name and different parameters. It is known as
method overloading.
12. Why method overloading is not Possible by changing the return type of method only?
Answer: Yes, we can overload main method in java by method overloading. You can have any no. of main
methods in java program. But JVM calls main method which receives string array as arguments.
Answer: If a subclass has same method as declared in the parent class, it is known as method overriding.
Method overriding is used for run-time polymorphism.
Answer: No, a static method cannot be overridden. It can be provided by the run-time polymorphism.
16. What are the differences between method overloading and method overriding?
Answer:
- method overloading is used to increase the readability of the program, while method overriding
is used to provide the specific implementation of the method.
- Method overloading performs within the class only, while method overriding occurs two classes.
- In case of method overloading, return type must be different. Whereas method overriding,
return type is same.
- Method overloading is the example of compile-time polymorphism, while method overriding is
the example of run-time polymorphism.
Answer: Super keyword is a reference variable which is used to refer the immediate parent class object.
Answer: Final keyword in java is a non-access modifier which is used to restrict the user. If you declare
any variable as final, you cannot change the value of final variable.
Answer: Yes, final method is inherited but you cannot override it.
Answer: Polymorphism in java is a concept by which we can perform single action in different ways.
There are two types of polymorphism in java: runtime polymorphism, and compile-time polymorphism.
Answer: Run-time polymorphism is also known as dynamic method dispatch, is a process in which a
method call to an overridden method is resolved at run-time rather than compile-time.
Answer: Static binding: When type of the object is determined at compiled time (by the compiler), it is
known as static binding.
Dynamic binding: When type of the object is determined at run-time, it is known as dynamic binding.
Answer: If a reference variable of the parent class is refers to the object of the child class, is called as
upcasting.
If a reference variable of the child class refers to the parent class object, is called as downcasting.
-Abstraction:
25. What is Abstraction?
Answer: Abstraction in java is a process of hiding all implementation details and showing only
functionality to the user.
For example, sending SMS, you type text and send it, you don’t know the internal process about the
message delivery.
Answer: abstract keyword is a non-access modifier which is used for classes and methods to achieve
abstraction.
A class which is declared with abstract keyword is known as abstract class. It can have abstract and non-
abstract methods.
A method which is declared as abstract and does not have implementation is known as an abstract
method.
Answer: Interface in java is a blue print of a class which is used to achieve abstraction and multiple
inheritance.
28. What are differences between the abstract class and Interface?
Answer:
- An abstract class can have abstract and non-abstract methods, while interface can have abstract
methods only.
- An abstract class can have static, non-static, final, non-final variables, while interfaces can have
final and abstract variables only.
- An abstract class does not supports multiple inheritance, while interfaces supports multiple
inheritance.
29. Multiple inheritance is not supported through class in java, but it is possible by an interface, why?
Answer: multiple inheritance is not supported in the case of class because of ambiguity. However, it is
supported in case of an interface because there is no ambiguity. It is because its implementation is
provided by the implementation class.
Answer: An interface which has no member is known as a marker or tagged interface, for
example, Serializable, Cloneable, Remote, etc.
Answer: A package is a folder which is collection of classes, methods, interfaces, abstract classes. There
are two types of packages – user-defined package and pre-defined or built-in package (the package
which is previously defined in java package API. For ex, java, awt, javax, swing etc.).
Answer: Access modifier in java specifies the accessibility or scope of a field, method, constructor or
class. There are 4 types of access modifiers in java.
- Private: can accessible only within the class. It cannot be accessed outside the class.
- Default: can accessible only within the package. Cannot be accessible outside the package.
- Protected: can accessible within the package and outside the package through the child class.
- Public: can be accessible everywhere i.e., inside and outside the package and inside and outside
the class.
Answer: Encapsulation in java is a process of wrapper code and data together into a single unit. For
example, a capsule is mixed with several medicines.
Answer:
Answer: The wrapper class in java is used to convert primitive data types into objects and vice versa.
Answer: The compile-time polymorphism is a polymorphism that is resolved during the compilation
process.
Answer: The java command-line argument is an argument i.e. passed at the time of running the java
program.
41. What is blank or uninitialized final variable?
Answer: A final variable that is not initialized at the time of declaration is known as blank final variable.
Answer: Recursion in java is a process in which a method calls itself continuously. It makes the code
compact but complex to understand.
Answer: A destructor is a special method which is automatically called when the object is made of scope
or destroyed.
Answer: ternary operator is the only conditional statement that takes three arguments. Arguments and
results are of different data types, and it depends on the function.