Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

A Complete Introduction to Abstract Classes in Java

Last updated on Nov 26,2019 2.4K Views

Ravi Kiran
Tech Enthusiast working as a Research Analyst at Edureka. Curious about learning... Tech Enthusiast working as a Research Analyst at Edureka. Curious about learning more about Data Science and Big-Data Hadoop.

Abstract Classes in Java help the users to achieve abstraction, which is the most crucial object-oriented programming practices followed during the process of software designing. In this article, we shall discuss the terminology of Abstract Classes through the following docket.

 

What are Abstract Classes in Java?

Abstract Classes in Java act as a boundary between the implementation method and its functionality. It is used to exchange the functionality between the Concrete class members and the Abstract Class.

 

Abstract-Classes-in-Java-Edureka

Abstract Classes are considered as those classes that hide the Method Implementation details from the user and show only the Method Functionality. They are declared using the keyword abstract. These methods can include Abstract and Non-Abstract methods in them.

 

Why do we need an Abstract Classes in Java?

We need Abstract Classes in Java for the following reasons:

  • Abstract Classes support Dynamic Method Resolution in run-time
  • They help users to achieve Loose Coupling
  • Abstract Classes separate the Method Definition from the Inherited Sub-Classes
  • They provide the Default Functionality of the defined method for all the Sub-Classes
  • Abstract classes provide a Template for future specific classes
  • The abstract class allows Code Re-usability

 

Why-we-need-Abstract-Classes-in-Java-Edureka-Abstact-Classes-in-Java

 

 

Rules for using Abstract Classes in Java

To implement an Abstract Class in Java, we need to follow the rules as described below:

  • An abstract class must be declared using the abstract keyword.
  • Abstract classes can include abstract and non-abstract methods.
  • An Abstract Class cannot be instantiated.
  • They can include constructors and static methods.
  • An Abstract Class includes final methods.

 

Rules-for-Abstract-Classes-in-Java-Edureka-Abstact-Classes-in-Java

 

 

Ways to achieve Abstraction in Java

The process of Abstraction in Java can be achieved by the following two methods as mentioned below:

  1. Implementing an Abstract Class
  2. Implementing an Interface 

 

The Syntax for Abstract Classes

The Syntax for defining Abstract Classes and Abstract Methods is as follows:

abstract class Edureka{} 
 abstract class Method(); 

 

 

Practical Examples of Abstract Classes

//Abstract Class

package Abstract;

public abstract class Person {
      private String name;
      private String gender;
      public Person(String nm, String gen) {
            this.name = nm;
            this.gender = gen;
      }
      public abstract void Studying();
      @Override
      public String toString() {
           return "Name=" + this.name + "::Gender=" + this.gender;
      }
}

//Student Class

package Abstract;

public class Student extends Person {
      private int StudentID;
      public Student(String nm, String gen, int id) {
            super(nm, gen);
            this.StudentID = id;
      }
      @Override
      public void Studying() {
            if (StudentID == 0) {
                  System.out.println("Not Studying");
            } 
            else {
                 System.out.println("Pursuing a Degree in Bachelor of Engineering");
            }
      }
      public static void main(String args[]) {
            Person student = new Student("Priya", "Female", 0);
            Person student1 = new Student("Karan", "Male", 201021);
            Person student2 = new Student("Kumari", "Female", 101021);
            Person student3 = new Student("John", "Male", 201661);
            student.Studying();
            student1.Studying();
            student2.Studying();
            student3.Studying();
            System.out.println(student.toString());
            System.out.println(student1.toString());
            System.out.println(student2.toString());
            System.out.println(student3.toString());
      }
}

Output:

Not Studying
Pursuing a Degree in Bachelor of Engineering
Pursuing a Degree in Bachelor of Engineering
Pursuing a Degree in Bachelor of Engineering
Name=Priya::Gender=Female
Name=Karan::Gender=Male
Name=Kumari::Gender=Female
Name=John::Gender=Male

 

Difference between Interface and Abstract Class

 

InterfaceAbstract Class
Can have only Abstract MethodsCan have Abstract and Non-Abstract Methods
It has only Final VariablesIt includes Non-Final Variables
It has Static and Final variables onlyIt has Static, Non-Static, final, Non-Final variables
Will not implement the Abstract ClassCan implement an Interface
Implemented using “implements” KeywordImplemented using “extends” Keyword
Can extend only an InterfaceCan extend Java Classes and Interfaces
Members are Public by defaultMembers can be Private and Protected

//Abstract Class Example

package abstactVSinterface;

abstract class Shape {
      String objectName = " ";
      Shape(String name) {
            this.objectName = name;
      }
      abstract public double area();
      abstract public void draw();
}
class Rectangle extends Shape {
      int length, width;
      Rectangle(int length, int width, String name) {
            super(name);
            this.length = length;
            this.width = width;
      }
      @Override
      public void draw() {
            System.out.println("Rectangle is drawn ");
      }
      @Override
      public double area() {
            return (double) (length * width);
      }
}
class Circle extends Shape {
      double pi = 3.14;
      int radius;
      Circle(int radius, String name) {
            super(name);
            this.radius = radius;
      }
      @Override
      public void draw() {
            System.out.println("Circle is drawn ");
      }
      @Override
      public double area() {
            return (double) ((pi * radius * radius) / 2);
      }
}
class Edureka {
      public static void main(String[] args) {
            Shape rect = new Rectangle(20, 30, "Rectangle");
            System.out.println("Area of rectangle: " + rect.area());
            Shape circle = new Circle(20, "Cicle");
            System.out.println("Area of circle is: " + circle.area());
      }
}

Output:

Area of rectangle: 600.0
Area of circle is: 628.0

//Interface Example

package absVSint;

interface Shape {
      void draw();
      double area();
}
class Rectangle implements Shape {
      int length, width;
      Rectangle(int length, int width) {
            this.length = length;
            this.width = width;
      }
      @Override
      public void draw() {
            System.out.println("Rectangle has been drawn ");
      }
      @Override
      public double area() {
            return (double) (length * width);
      }
}
class Circle implements Shape {
      double pi = 3.14;
      int radius;
      Circle(int radius) {
            this.radius = radius;
      }
      @Override
      public void draw() {
            System.out.println("Circle has been drawn ");
      }
      @Override
      public double area() {
            return (double) ((pi * radius * radius) / 2);
      }
}
class Edureka {
      public static void main(String[] args) {
            Shape rect = new Rectangle(20, 30);
            System.out.println("Area of rectangle: " + rect.area());
            Shape circle = new Circle(20);
            System.out.println("Area of circle: " + circle.area());
      }
}

Output:

Area of rectangle: 600.0
Area of circle: 628.0

 

With this, we come to an end of this article. I hope you have understood the importance of Abstraction, Syntax, functionality, Rules of Abstraction in Java and practical examples related to them.

Now that you have understood basics of Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Mention it in the comments section of this “Abstract Classes in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

A Complete Introduction to Abstract Classes in Java

edureka.co