Java OOPs

Q. What is an object-oriented paradigm?
Paradigm literally means a pattern or a method. 
Programming paradigms are the methods to solve a program that is of four types namely, Imperative, logical, functional, and object-oriented. 
When objects are used as base entities upon which the methods are applied, encapsulation or inheritance functionalities are performed, it is known as an object-oriented paradigm.

Q. What is the difference between an object-oriented programming language and an object-based programming language?
Object-oriented programming language covers larger concepts like inheritance, polymorphism, abstraction, etc.
It supports all the built-in objects
  Ex: Java, C#, etc.
The scope of object-based programming is limited to the usage of objects and encapsulation.
It doesn’t support all the built-in objects
  Ex: JavaScript, Visual Basics, etc.

Q. What are the main concepts of OOPs in Java?
The main concepts of OOPs in Java are mentioned below:
  Inheritance
  Polymorphism
  Abstraction
  Encapsulation 

Q. Define Inheritance.
'IS-A' is a type of relationship in OOPs Java where one class inherits another class.
When an object that belongs to a subclass acquires all the properties and behavior of a parent object that is from the superclass, it is known as inheritance.
A class within a class is called the subclass and the latter is referred to as the superclass. 
Sub class or the child class is said to be specific whereas the superclass or the parent class is generic.
Inheritance provides code reusability.
Inheritance is the method by which the Child class can inherit the features of the Super or Parent class. In Java, Inheritance is of four types:
1. Single Inheritance
  When a child or subclass extends only one superclass, it is known to be single inheritance. Single-parent class properties are passed down to the child class.
2. Multilevel Inheritance
  When a child or subclass extends any other subclass a hierarchy of inheritance is created which is known as multilevel inheritance. In other words, one subclass becomes the parent class of another.
3. Hierarchical Inheritance: When multiple subclasses derive from the same parent class is known as Hierarchical Inheritance. In other words, a class that has a single parent has many subclasses.
4. Multiple Inheritance: When a child class inherits from multiple parent classes is known as Multiple Inheritance. In Java, it only supports multiple inheritance of interfaces, not classes.

Q. What do you mean by data encapsulation?
Data Encapsulation is the concept of OOPS properties and characteristics of the classes that The interface is binded together.
Basically, it bundles data and methods that operate on that data within a single unit.
Encapsulation is achieved by declaring the instance variables of a class as private, which means they can only be accessed within the class.
The advantages of Encapsulation in Java are mentioned below:
1. Data Hiding: it is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding. The user will have no idea about the inner implementation of the class.
2. Increased Flexibility: We can make the variables of the class read-only or write-only depending on our requirements.
3. Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.
4. Testing code is easy: Code is made easy to test for unit testing.
The main advantage of Encapsulation in Java is its ability to protect the internal state of an object from external modification or access. 
It is the is a way of hiding the implementation details of a class from outside access and only exposing a public interface that can be used to interact with the class. 
The main benefit is of providing a way to control and manage the state and the behavior of an object and also protecting it from modification and unauthorized access at the same time.

Q. What do you mean by aggregation?
Aggregation is a term related to the relationship between two classes best described as a "has-a" relationship.
This kind is the most specialized version of association.
It is a unidirectional association means it is a one-way relationship.
It contains the reference to another class and is said to have ownership of that class.

Q. What is an association?
The association is a relation between two separate classes established through their Objects. 
It represents Has-A’s relationship.

Q: Is Java Platform Independent if then how?
Yes, Java is a Platform Independent language. 
Unlike many programming languages javac compiles the program to form a bytecode or .class file. 
This file is independent of the software or hardware running but needs a JVM(Java Virtual Machine) file preinstalled in the operating system for further execution of the bytecode.
Although JVM is platform dependent, the bytecode can be created on any System and can be executed in any other system despite hardware or software being used which makes Java platform independent.

Q. What is an Interface?
An interface in Java is a collection of static final variables and abstract methods that define the contract or agreement for a set of linked classes.
Any class that implements an interface is required to implement a specific set of methods.
It specifies the behavior that a class must exhibit but not the specifics of how it should be implemented.
Features of the Interface are mentioned below:
1. The interface can help to achieve total abstraction.
2. Allows us to use multiple inheritances in Java.
3. Any class can implement multiple interfaces even when one class can extend only one class.
4. It is also used to achieve loose coupling.

Q. What is a marker interface?
An Interface is recognized as an empty interface (no field or methods) it is called a marker interface.
Examples of marker interfaces are Serializable, Cloneable, and Remote interfaces.

Q. What are the differences between abstract class and interface?
Abstract Class:
Both abstract and non-abstract methods may be found in an abstract class.
Abstract Class supports Final methods.
Multiple inheritance is not supported by the Abstract class.
Abstract Keyword is used to declare Abstract class.
extend keyword is used to extend an Abstract Class.
Abstract Class has members like protected, private, etc.
Interface Class:
The interface contains only abstract methods.
The interface class does not support Final methods.
Multiple inheritances is supported by Interface Class.
Interface Keyword is used to declare the interface class.
implements keyword is used to implement the interface.
All class members are public by default.

Q. What are operators?
Operators are the special types of symbols used for performing some operations over variables and values.
All types of operators in Java are mentioned below:
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator

Q. What is covariant return type?
The covariant return type specifies that the return type may vary in the same direction as the subclass.
It's possible to have different return types for an overriding method in the child class, but the child’s return type should be a subtype of the parent’s return type and because of that overriding method becomes variant with respect to the return type.
We use covariant return type because of the following reasons:
1. Avoids confusing type casts present in the class hierarchy and makes the code readable, usable, and maintainable.
2. Gives liberty to have more specific return types when overriding methods.
3. Help in preventing run-time ClassCastExceptions on returns.

Q. What is the transient keyword?
The transient keyword is used at the time of serialization if we don’t want to save the value of a particular variable in a file.
When JVM comes across a transient keyword, it ignores the original value of the variable and saves the default value of that variable data type.

Q. Where and how can you use a private constructor?
A private constructor is used if you don't want any other class to instantiate the object to avoid subclassing.

Q. What is the difference between static (class) method and instance method?
Static(Class) method
:
Static method is associated with a class rather than an object.
Static methods can be called using the class name only without creating an instance of a class.
Static methods do not have access to this keyword.
Static methods can access only static members of the class.
Static methods cannot be overridden because they are resolved at compile time, not at run time. This means that the compiler decides which method to call based on the reference type, not on the object type.
Instance method:
The instance method is associated with an object rather than a class.
The instance methods can be called on a specific instance of a class using the object reference.
Instance methods have access to this keyword.
Instance methods can access both static and non-static methods of the class.
Instance methods can be overridden because they are resolved at run time, not at compile time. This means that the compiler decides which method to call based on the object type, not on the reference type.

Q. What is this keyword in Java?
‘this’ is a keyword used to reference a variable that refers to the current object.

Q. What are the advantages of passing this into a method instead of the current class object itself?
There are a few advantages of passing this into a method instead of the current class object itself these are:
1. this is the final variable because of which this cannot be assigned to any new value whereas the current class object might not be final and can be changed.
2. this can be used in the synchronized block.

Q. What are Brief Access Specifiers and Types of Access Specifiers?
Access Specifiers in Java help to restrict the scope of a class, constructor, variable, method, or data member.
There are four types of Access Specifiers in Java mentioned below:
Public
Private
Protected
Default

Q. What are the differences between the constructors and methods?
Java constructors are used for initializing objects. During creation, constructors are called to set attributes for objects apart from this few basic differences between them are:
1. Constructors are only called when the object is created but other methods can be called multiple times during the life of an object.
2. Constructors do not have a return type, whereas methods have a return type, which can be void or any other type.
3. Constructors are used to setting up the initial state but methods are used to perform specific actions.

Q. How many ways you can take input from the console?
There are two methods to take input from the console in Java mentioned below:
Using Command line argument
Using Buffered Reader Class
  BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
Using Console Class
  String x = System.console().readLine();
Using Scanner Class
  Scanner in = new Scanner(System.in);
  String str = in.nextLine();


Popular posts from this blog

IT Software Services

Java Architect

Agile Ceremonies