Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Encapsulation::

Binding of data members with methods.


Making them private and using thru getters and setters.

Why or advantage::

By providing only a setter or getter method, you can make the class read-only or
write-only. In other words, you can skip the getter or setter methods.

It provides you the control over the data. Suppose you want to set the value of id
which should be greater than 100 only, you can write the logic inside the setter
method. You can write the logic not to store the negative numbers in the setter
methods.

It is a way to achieve data hiding in Java because other class will not be able to
access the data through the private data members.

The encapsulate class is easy to test. So, it is better for unit testing.

The standard IDE's are providing the facility to generate the getters and setters.
So, it is easy and fast to create an encapsulated class in Java.

Polymosphysm::

Polymorphism in Java is a concept by which we can perform a single action in


different ways.
Can be achieved in 2 ways: Compile time polymor(overloading) and Runtime
poly(overriding).

*Cannot override private and static method, static method is bind with class
refernece so it is method hiding.

*Can overload main method, but JVM will auto run real main method with
main(String[] args).
*Cannot overide main, as it is static.

Inheritance::

Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).

Abstraction::

Data abstraction is the process of hiding certain details and showing only
essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces.

Abstracat Class::

An abstract class in Java is one that is declared with the abstract keyword. It may
have both abstract and non-abstract methods(methods with bodies). An abstract is a
java modifier applicable for classes and methods in java but not for Variables.

Interface::
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only


abstract methods in the Java interface, not method body. It is used to achieve
abstraction and multiple inheritance in Java.

The reason is, abstract classes may contain non-final variables, whereas variables
in the interface are final, public and static.

When to use abstract class::

there are some related classes that need to share some lines of code then you can
put these lines of code within the abstract class and this abstract class should be
extended by all these related classes.

An abstract class is a good choice if we are using the inheritance concept since it
provides a common base class implementation to derived classes.

An abstract class is also good if we want to declare non-public members. In an


interface, all methods must be public.

If we want to add new methods in the future, then an abstract class is a better
choice. Because if we add new methods to an interface, then all of the classes that
already implemented that interface will have to be changed to implement the new
methods.

When to use interface::

If the functionality we are creating will be useful across a wide range of


disparate objects, use an interface. Abstract classes should be used primarily for
objects that are closely related, whereas interfaces are best suited for providing
a common functionality to unrelated classes.

Interfaces are a good choice when we think that the API will not change for a
while.

Interfaces are also good when we want to have something similar to multiple
inheritances since we can implement multiple interfaces.

*Hashcode and equals::

If hascode of 2 objects is same it doesnt mean they are equal.


If two objects are the same as per the equals(Object) method, then if we call the
hashCode() method on each of the two objects, it must provide the same integer
result.

**In object class, equals check reference but in string classs, it checks value.
String buffer and builder is also same as Object class.

****
Although its an old question let me give my input on it as well.

abstract class: Inside abstract class we can declare instance variables, which are
required to the child class

Interface: Inside interface every variables is always public static and final we
cannot declare instance variables

abstract class: Abstract class can talk about state of object

Interface: Interface can never talk about state of object

abstract class: Inside Abstract class we can declare constructors

Interface: Inside interface we cannot declare constructors as purpose of


constructors is to initialize instance variables. So what is the need of
constructor there if we cannot have instance variables in interfaces.

abstract class: Inside abstract class we can declare instance and static blocks

Interface: Interfaces cannot have instance and static blocks.

abstract class: Abstract class cannot refer lambda expression

Interfaces: Interfaces with single abstract method can refer lambda expression

abstract class: Inside abstract class we can override OBJECT CLASS methods

Interfaces: We cannot override OBJECT CLASS methods inside interfaces.

I will end on the note that:

Default method concepts/static method concepts in interface came just to save


implementation classes but not to provide meaningful useful implementation. Default
methods/static methods are kind of dummy implementation, "if you want you can use
them or you can override them (in case of default methods) in implementation class"
Thus saving us from implementing new methods in implementation classes whenever new
methods in interfaces are added. Therefore interfaces can never be equal to
abstract classes.

You might also like