Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

How to Implement Data Abstraction in Java?

Updated on 29 September, 2022

5.93K+ views
7 min read

Abstraction, polymorphism, encapsulation, and inheritance are the four basic elements of object-oriented programming. One of them, Data Abstraction, will be discussed in detail in the following article. We will also learn how to perform data abstraction in Java.

What is Data Abstraction?

Data abstraction is the characteristic feature where just the most important details are shown to the user. The user is kept unaware of the trivial or non-essential details. For example, an automobile is considered as a whole rather than its distinct parts. The practice of identifying only the needed attributes of an item while discarding the extraneous information is known as data abstraction. 

In simple terms, data abstraction displays important items to the user while keeping insignificant elements hidden.

We use the keyword “abstract” to implement abstraction with the help of classes and interfaces. We can have both abstract and concrete methods in abstract classes.

What are Abstract Classes?

In Object-Oriented Programming, an abstract class is a class that declares one or more abstract methods. The sole distinction between abstract and regular Java classes is that abstract classes contain the abstract keyword, whereas regular Java classes do not. For the declaration of an abstract class, we use the abstract keyword before its class name. There can be both abstract and concrete methods in classes. However, abstract methods can’t be present in regular classes. A class with at least one abstract method is referred to as an abstract class.

Its syntax is: 

public abstract class Name_of_Class  

{  

public abstract Name_of_Method();  

}  

 

An example of implementing abstract class is: 

 

//abstract class naming

abstract class DemoClass  

{  

//abstract method naming

abstract void disp();  

}  

//proceeding with the abstract class  

public class MainClass extends DemoClass  

{  

//defining the abstract class’s method body

void display()  

{  

System.out.println(“Abstract method called.”);  

}  

public static void main(String[] args)  

{    

MainClass obj = new MainClass ();  

//invoking abstract method  

obj.display();  

}  

}  

To list pointwise: 

  • A class must be defined as abstract if it has at least one abstract method.
  • A class that has been declared abstract cannot be instantiated.
  • You must inherit an abstract class from another class and supply implementations for its abstract methods to utilise it.
  • You must supply implementations for all abstract methods in an abstract class if you inherit it.

What are Abstract Methods?

An abstract method has only the method declaration and no implementation. An abstract method can be represented by a method without a body of its own. Abstract methods must be declared exclusively within abstract classes.

If you want a class to have a method but the actual implementation of that method to be determined by child classes, designate the method as an abstract in the parent class.

Advantages of Data Abstraction in Object-Oriented Programming

  • In Object-Oriented Programming, abstraction reduces the complexity of the programme design and the process of its implementation on software.
  • The foremost advantage of implementing abstraction in Java programming is that users can then easily organise classes based on similarities, such as siblings, and therefore, inheritance of data and migration of data becomes easy and feasible.
  • Inheritance is possible. For example: 

/* File name : DemoAbstract.java */

public class DemoAbstract {
   public static void main(String [] args) {
      /* the following cannot be written and initiate errors */
      employee e = new employee(“Peter D.”, “Hartford, CT”, 35);
      System.out.println(“\n Use Employee reference and call MailCheck–“);
      e.mailCheck();
   }
}

This shows the following error:

Employee.java:46: Employee is abstract; cannot be instantiated

      employee e = new employee(“Peter D.”, “Hartford, CT”, 35);
                   ^
1 error

So we use the abstract class usage as:

/* File name : employee.java */
public abstract class employee {
   private String N;
   private String Add;
   private int Num;

   public Employee(String N, String Add, int Num) {
      System.out.println(“Constructing an employee”);
      this.name = N;
      this.address = Add;
      this.number = Num;
   }
   
   public double compute_pay() {
     System.out.println(“Inside employee compute_pay”);
     return 0.0;
   }
   
   public void Mailcheck() {
      System.out.println(“Mailing a check to ” + this.name + ” ” + this.address);
   }

   public String toString() {
      return N + ” ” + Add + ” ” + N;
   }

   public String getName() {
      return N;
   }
 
   public String getAddress() {
      return Add;
   }
   
   public void setAddress(String newAddress) {
      Add = newAddress;
   }
 
   public int getNumber() {
      return Num;
   }
}

When do we use abstract class or abstract methods? 

When we consider the general  example of “shapes”, we may imagine it being used in reference to a computer-aided design system or for a video game simulation. The fundamental type referred to here is a “shape,” with every shape having its own color attributes, size, etc. Specific classes of shapes-circle, square, triangle, etc., are derived (inherited) from this, each of which may have extra unique properties and behaviors. Certain shapes, for example, can be symmetrical, while others are not. Both the similarities and distinct differences between the shapes are embodied in the type hierarchy.

Therefore, shape can be analogous to an abstract class, while the different types of shapes can be denoted as concrete classes. 

Here is a real-life scenario to explain abstract classes:

// Java program to demonstrate Abstraction

abstract class shape {

String colour;

 

abstract double area();

public abstract String toString();

 

// abstract class can have the constructor

public shape(String colour)
{
System.out.println(“Shape constructor called”);
this.colour = colour;
}

// this is a concrete method
public String getColour() { return colour; }
}
class Circle extends shape {
double r;

public Circle(String colour, double(r)
{

// calling Shape constructor
super(colour);
System.out.println(“Circle constructor called”);
this.r = r;
}

@Override double area()
{
return Math.PI * Math.pow(r, 2);
}

@Override public String toString()
{
return “Circle color is ” + super.getColor()
+ “and area is : ” + area();
}
}
class Rectangle extends shape {

double length;
double width;

public Rectangle(String colour, double length,
double width)
{
// calling Shape constructor
super(colour);
System.out.println(“Rectangle constructor called”);
this.length = length;
this.width = width;
}

@Override double area() { return length * width; }

@Override public String toString()
{
return “Rectangle colour is ” + super.getcolor()
+ “and area is : ” + area();
}
}
public class Test {
public static void main(String[] args)
{
Shape s1 = new Circle(“Red”, 2.2);
Shape s2 = new Rectangle(“Yellow”, 2, 4);

System.out.println(s1.toString());
System.out.println(s2.toString());
}
}

This segment of code is a modified version of the one here.

A code snippet of a program where data abstraction in Java is used:

 // Abstraction in Java
abstract class ElecBill
{
    //abstract method
    abstract float compBill();
}

class Comm extends ElecBill
{
    
    float compBill() {
        return 1.00*100;
    }
}
class Domestic extends ElecBill
{
    float compBill() {
        return 0.50*75;
    }
}

Conclusion

Data abstraction is an important aspect of object-oriented programming. For languages like Java, abstraction and other OOP concepts like inheritance, encapsulation, and polymorphism play a crucial role in writing efficient programs.

If you’d like to learn OOPs in in-depth and acquire first-class programming skills, we recommend joining upGrad’s Job-linked PG Certification in Software Engineering which is designed to help students acquire skills in Java, OODAP, DSA, HTML5, CSS3, JavaScript, MERN, AWS, SQL & NoSQL Databases, Spring Boot, etc. The 5-month course covers two specialisations – MERN Stack Specialization and Cloud-Native Specialization and provides access to upGrad 360° career counselling sessions.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

1. What is polymorphism? How is it different from data abstraction?

1. What is polymorphism? How is it different from data abstraction?

Polymorphism is a generalization, similar to data abstraction, but it occurs at runtime. If there is a mechanism to access a collection of distinct object types in which they are indistinguishable from one another, they are polymorphic. So, even if they are not identical, all of the objects within have the same appearance and feel. The goal is to drastically decrease coding. Instead of writing all of the numerous permutations for each individual type, you can write one universal solution.

2. What are the differences between Encapsulation and Data Abstraction?

ENCAPSULATION - 1. Encapsulation hides data (information), whereas abstraction hides details (implementation hiding). 2. Object-Oriented Programming's abstraction solves problems at the design level. 3. For security purposes, encapsulation implies hiding the internal information or mechanics of how an object performs something. ABSTRACTION - 1. While encapsulation binds together data and methods that operate on it, data abstraction is concerned with exposing the user interface while obscuring implementation details. 2. It is solved at the implementation level through encapsulation. 3. In Java, data abstraction allows you to concentrate on what the information object must include.

3. What are the distinguishing features between interface and abstract class?

Interface - 1. Only abstract methods find themselves to be a part of the interface. 2. Multiple inheritances are supported. 3. It is unable to offer the abstract class's implementation. 4. Only public abstract methods are allowed in an interface. 5. Only one public static final variable can be used in the interface. Abstract class - 1. Abstract, as well as non-abstract methods, can be found in an abstract class. 2. Multiple inheritances are not supported. 3. It is capable of implementing the interface. 4. Protected and abstract public methods are both possible in an abstract class. 5. With any access specifier, an abstract class can have final, static, or static final variables.

RELATED PROGRAMS