SlideShare a Scribd company logo
1 of 15
Abstraction
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.
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.
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.
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.
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.
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
• }
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.
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.
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
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.
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();
• }
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
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
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.

More Related Content

What's hot

What's hot (20)

Nested class in java
Nested class in javaNested class in java
Nested class in java
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
OOP java
OOP javaOOP java
OOP java
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Io streams
Io streamsIo streams
Io streams
 
Java swing
Java swingJava swing
Java swing
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java packages
Java packagesJava packages
Java packages
 

Similar to Abstraction in java.pptx

More oop in java
More oop in javaMore oop in java
More oop in javaSAGARDAVE29
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdfSudhanshiBakre1
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078Aravind NC
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun Vasanth
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdfParvizMirzayev2
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingWondimuBantihun1
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 eehomeworkping9
 
Abstract classes and Methods in java
Abstract classes and Methods in javaAbstract classes and Methods in java
Abstract classes and Methods in javaHarish Gyanani
 
Abstract Class and Interface.pdf
Abstract Class and Interface.pdfAbstract Class and Interface.pdf
Abstract Class and Interface.pdfrajaratna4
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
When to use abstract class and methods in java
When to use abstract class and methods in java When to use abstract class and methods in java
When to use abstract class and methods in java kritikumar16
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Simplilearn
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics PresentationOmid Sohrabi
 

Similar to Abstraction in java.pptx (20)

More oop in java
More oop in javaMore oop in java
More oop in java
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Oop
OopOop
Oop
 
Abstract classes and Methods in java
Abstract classes and Methods in javaAbstract classes and Methods in java
Abstract classes and Methods in java
 
Abstract Class and Interface.pdf
Abstract Class and Interface.pdfAbstract Class and Interface.pdf
Abstract Class and Interface.pdf
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
When to use abstract class and methods in java
When to use abstract class and methods in java When to use abstract class and methods in java
When to use abstract class and methods in java
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Unit 4
Unit 4Unit 4
Unit 4
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 

Recently uploaded

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 

Recently uploaded (20)

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 

Abstraction in java.pptx

  • 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.