2. Abstraction in Java
• Data Abstraction is the property by virtue of which only the essential
details are displayed to the user.
• Data Abstraction may also be defined as the process of identifying
only the required characteristics of an object ignoring the irrelevant
details.
• The properties and behaviors of an object differentiate it from other
objects of similar type and also help in classifying/grouping the
objects.
• In java, abstraction is achieved by interfaces and abstract classes. We
can achieve 100% abstraction using interfaces.
3. Abstract classes and Abstract methods :
• An abstract class is a class that is declared with an abstract keyword.
• An abstract method is a method that is declared without implementation.
• An abstract class may or may not have all abstract methods. Some of them can be
concrete methods
• A method-defined abstract must always be redefined in the subclass, thus
making overriding compulsory or making the subclass itself abstract.
• Any class that contains one or more abstract methods must also be declared with an
abstract keyword.
• There can be no object of an abstract class. That is, an abstract class can not be
directly instantiated with the new operator.
• An abstract class can have parameterized constructors and the default constructor is
always present in an abstract class.
4. When to use abstract classes and abstract methods
• There are situations in which we will want to define a superclass that declares
the structure of a given abstraction without providing a complete
implementation of every method.
• Sometimes we will want to create a superclass that only defines a
generalization form that will be shared by all of its subclasses, leaving it to
each subclass to fill in the details.
5. Advantages of Abstraction
• It reduces the complexity of viewing things.
• Avoids code duplication and increases reusability.
• Helps to increase the security of an application or program as only
essential details are provided to the user.
• It improves the maintainability of the application.
• It improves the modularity of the application.
• The enhancement will become very easy because without affecting
end-users we can able to perform any type of changes in our internal
system.
6. Encapsulation vs Data Abstraction
• Encapsulation is data hiding(information hiding) while Abstraction is
detailed hiding(implementation hiding).
• While encapsulation groups together data and methods that act upon
the data, data abstraction deal with exposing the interface to the user
and hiding the details of implementation.
• Encapsulated classes are java classes that follow data hiding and
abstraction while We can implement abstraction by using abstract
classes and interfaces.
• Encapsulation is a procedure that takes place at the implementation
level, while abstraction is a design-level process.
7. abstract keyword in java
• abstract is a non-access modifier in java applicable for classes, methods but not variables.
• It is used to achieve abstraction which is one of the pillar of Object Oriented
Programming(OOP).
• Following are different contexts where abstract can be used in Java.
• Abstract classes
• The class which is having partial implementation(i.e. not all methods present in the class
have method definition).
• To declare a class abstract, use this general form :
•
• abstract class class-name{
• //body of class
• }
8. Why no instantiation for abstract classes
• Due to their partial implementation, we cannot instantiate abstract
classes.
• Any subclass of an abstract class must either implement all of the
abstract methods in the super-class, or be declared abstract itself.
• Some of the predefined classes in java are abstract.
• They depend on their sub-classes to provide complete
implementation.
For example, java.lang.Number is a abstract class.
9. Abstract methods
• Sometimes, we require just method declaration in super-classes.
• This can be achieve by specifying the abstract type modifier. These methods are
sometimes referred to as subclasser responsibility because they have no
implementation specified in the super-class.
• Thus, a subclass must override them to provide method definition.
• To declare an abstract method, use this general form:
• abstract type method-name(parameter-list);
• Any concrete class(i.e. class without abstract keyword) that extends an abstract class
must overrides all the abstract methods of the class.
10. Important rules for abstract methods:
• Any class that contains one or more abstract methods must also be declared
abstract
• The following are various illegal combinations of other modifiers for methods
with respect to abstract modifier :
• final
• abstract static
• abstract private
11. abstract with final : legal or illegal
• In java, you will never see a class or method declared with both final and
abstract keywords.
• For classes, final is used to prevent inheritance whereas abstract classes
depends upon their child classes for complete implementation.
• In cases of methods, final is used to prevent overriding whereas abstract
methods needs to be overridden in sub-classes.
12. Abstract Class in Java
• 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.
• abstract class Shape
• {
• int color;
• // An abstract function
• abstract void draw();
• }
13. Abstract class observations
• An instance of an abstract class can not be created.
• Constructors are allowed.
• We can have an abstract class without any abstract method.
• There can be a final method in abstract class but any abstract method in
class(abstract class) can not be declared as final or in simpler terms final method can
not be abstract itself as it will yield an error: “Illegal combination of modifiers:
abstract and final”
• We can define static methods in an abstract class
• We can use the abstract keyword for declaring top-level classes (Outer class) as well
as inner classes as abstract
• If a class contains at least one abstract method then compulsory should declare a
class as abstract
• If the Child class is unable to provide implementation to all abstract methods of
the Parent class then we should declare that Child class as abstract so that the next
level Child class should provide implementation to the remaining abstract method
14. Abstraction cases
1. In Java, just likely in C++ an instance of an abstract class cannot be created, we can
have references to abstract class type though.
2. Like C++, an abstract class can contain constructors in Java. And a constructor of an
abstract class is called when an instance of an inherited class is created.
3. In Java, we can have an abstract class without any abstract method. This allows us
to create classes that cannot be instantiated but can only be inherited.
4. Abstract classes can also have final methods (methods that cannot be overridden).
5. For any abstract java class we are not allowed to create an object i.e., for abstract
class instantiation is not possible but reference of base possible using child class
constructor.
6. we can define static methods in an abstract class that can be called independently
without an object.
7. We can use the abstract keyword for declaring top-level classes (Outer class) as
well as inner classes as abstract
15. Abstraction cases
8.If a class contains at least one abstract method then compulsory we should
declare the class as abstract otherwise we will get a compile-time error because
If a class contains at least one abstract method then, implementation is not
complete for that class, and hence it is not recommended to create an object so
in order to restrict object creation for such partial classes we use abstract
keyword.
9. If the Child class is unable to provide implementation to all abstract
methods of the Parent class then we should declare that Child class as
abstract so that the next level Child class should provide implementation to the
remaining abstract method.