OOPS CONCEPTS
Abstraction: Abstraction refers to the ability of a class to hide its internal
implementation details and expose only its essential features to the outside
world. This allows users of an object of that class to interact with it without
needing to know its internal workings.
Encapsulation: Encapsulation refers to the idea of keeping the internal state
(i.e. current values of attributes) and behavior of an object hidden from the
outside world while providing a public interface for interacting with it. This
allows the class or object to control access to its internal data and behavior,
and protect it from being modified or accessed in an unintended way.
"Encapsulation" - binding code and data into single unit. By making your
variables private and define public methods(getter and setter) for
accessing and modifying the data.
"Synchronised" - method declared with synchronised keyword is mainly
used if multithreading concept is involved, only one thread is allowed to
enter the method and only after a thread's complete execution, the other
thread can start it's execution.
Inheritance: Two classes (say Car, and Toyota) have the inheritance
relationship between them when one class, say Toyota, is extending the other
class (Car). In this example, Car will be referred to as a base-class or
a superclass whereas Toyota will be referred to as a derived-
class or subclass. Inheritance allows a derived class to take some of the
attributes and methods of the base class. Moreover, the derived class may
enhance the base class by introducing additional attributes and methods.
Inheritance promotes code reusability and extensibility.
Polymorphism: Polymorphism allows objects of different classes to be
treated as objects of one of their common superclasses. This enables
methods to be defined on the superclass to be overridden by subclasses to
provide their own implementation. Thus a single method call can be
executed on objects of multiple classes, with the appropriate implementation
being called based on the actual class of the object.
What is a constructor ?
A constructor is a special function of a class which is called automatically upon
creation of an object. Thus, a constructor usually outline the steps that occur when
one instance of a class is created.
A constructor’s name must be the same as it’s class name.
Put simply, constructors can create and initialize objects of their class type. They
cannot be classified as volatile or static nor can the body of a constructor contain
a return statement.
Why Multiple inheritance is not supported in Java?
The reason behind this is to prevent ambiguity.
Consider a case where class B extends class A and Class C and both class A and C have the
same method display().
Now java compiler cannot decide, which display method it should inherit. To prevent such
situation, multiple inheritances is not allowed in java.
Why Multiple inheritance is supported by Interface ?
An interface is a fully abstract class i.e., it includes a set of abstract methods that
specifies what a class must do and not how to do it.
Hence, if a class implements an interface, it inherits all the abstract methods of
the interface and provides an implementation for each of them. interfaces do not
contain detailed instructions for their behaviors. Because of this feature, a class
can implement any number of interfaces without any ambiguity as the
implementation is provided by the class itself.
types of Inheritance in Java.
Single-level inheritance.
Multi-level Inheritance.
Hierarchical Inheritance.
Multiple Inheritance.
Hybrid Inheritance.
Using Object Class Methods
The Object class provides multiple methods which are as follows:
tostring() method
hashCode() method
equals(Object obj) method
finalize() method
getClass() method
clone() method
wait(), notify() notifyAll() methods