Professional Documents
Culture Documents
OOPS
OOPS
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::
*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 reason is, abstract classes may contain non-final variables, whereas variables
in the interface are final, public and static.
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.
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.
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.
**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: Inside abstract class we can declare instance and static blocks
Interfaces: Interfaces with single abstract method can refer lambda expression
abstract class: Inside abstract class we can override OBJECT CLASS methods