Java Abstract Class Overview Explained with Examples – MPS

Java Abstract Class Overview with Examples

In this lecture, we will explain to you the concept of the Java abstract class. There are follow-up examples to get into the thick and thin of what the abstraction does.
Java Abstract Class Overview With Examples
Java Abstract Class
Abstract class in Java is like a template for functions that have meanings outside. However, unlike the interfaces, it can also have some rules that could have code inside.

What does ABSTRACTION mean?

In object-situated programming (OOP), deliberation turns out to be one of the essential highlights, be it Java or some other OOP language. It is the system or cycle of introducing to the client just working subtleties while concealing the execution subtleties of the design. For instance, when you peruse the Internet, you are just worried about the location or space name of a specific site page. When you enter the location, your program needs to bring subtleties and communicate with workers across the globe. It’s anything but for the end-client to pick how the convention of correspondence happens between the program and the workers. Returning to Java, reflection permits the software engineer to conceal explicit subtleties from the client while referencing others. The client just has data about what the article does however doesn’t have a clue how it occurs. We can carry out Java reflection with the accompanying two ideas:
  • Abstract Classes
  • Interfaces in Java
We will talk about Abstract Classes in this tutorial.

1. Abstract Class in Java Example

An abstract class is one that has its capacities characterized in different classes. Thus, we call it Abstract, which functions as a format.The contrast between a normal class and an abstract class emerges with the catchphrase “abstract.” To characterize a class abstract, the programmer should utilize the abstract watchword prior to pronouncing the class.We can’t start up an abstract class. It implies we can’t make objects for an abstract class.Assuming you need to burn-through the usefulness of the abstract classes, get another class broadening it. You can accomplish this conduct by utilizing the “broadens” watchword. We term this cycle as acquiring.The acquiring class ought to give appropriate execution subtleties to every one of the abstract methods in the abstract class.An_abstract class can have normal just as abstract methods. An abstract method can’t be announced without an abstract_class, while the converse is conceivable.The show for class factors is to be set to private while we can skip it in the event that we need. The acquiring class can have its own arrangement of methods, yet it’s anything but a body for all abstract methods in the abstract class.

Abstract Methods in Java

An abstract method is characterized inside an abstract class when the programmer needs the class to contain a method yet doesn’t have any desire to give genuine execution subtleties to it.You ought to compose the abstract catchphrase before the method to characterize any method abstract. Additionally, you can announce an abstract method in the abstract class. Be that as it may, the acquiring class should have its definition.By proclaiming, we mean determining only the mark of the method. The body of the method or the definition is accessible in the broadening class.To characterize an abstract class, after the mark of the method, we put a semi-colon (;), rather than bracket.

Abstraction Examples

In this segment, we’ve raised a few guides to exhibit how abstraction functions in Java. You can utilize the code given here and practice from the Java order line or utilizing the Eclipse IDE.

Example-I

So, let’s create a Java abstract_class. It has one regular and one abstract method.
abstract class AbstractClass 
{
   private String a;
   int b;

   AbstractClass(String label, int num) 
  {
      a = label;
      b = num;
   }

   String getLabel() 
 {
      return a;
   }
   abstract int getNumber();   
}
In the above bit, the AbstractClass has getNumber() as its abstract method while getName is a standard method. You can’t start up an object of this class. Along these lines, you ought to make a substantial class that will acquire the abstract class.
class ConcreteClass extends AbstractClass 
{

   ConcreteClass(String label, int num) 
   {
      super(label, num);
   }
   int getNumber() 
  {
      return b;
   }
}
Presently, we can utilize the methods of AbstractClass in the wake of launching the ConcreteClass.On the off chance that you see, the ConcreteClass constructor contains the super watchword. It calls AbstractClass constructor with the accompanying boundaries:
public class Main 
{
   public static void main(String args[]) 
  {
      ConcreteClass obj = new ConcreteClass("AJ", 1); 
      System.out.println( obj.getNumber() );
      System.out.println( obj.getLabel() );
   }
}
Output:
abstract class AbstractClass 
{
   private String a;
   int b;

   AbstractClass(String label, int num) 
  {
      a = label;
      b = num;
   }

   String getLabel() 
{
      return a;
   }
   abstract int getNumber();   
}

class ConcreteClass extends AbstractClass 
{
   ConcreteClass(String label, int num) 
{
      super(label, num);
   
   int getNumber() 
      return b;
   }
}


public class Main 
{
   public static void main(String args[]) 
  {
      ConcreteClass obj = new ConcreteClass("AJ", 1); 
      System.out.println( obj.getNumber() );
      System.out.println( obj.getLabel() );
   }
}
After running the above Java code, you would see the output as shown below:
1
AJ

Java abstract class Example-II

Look at another guide to get greater clearness. It thinks about one abstract class, Area, and two different subclasses broadening it. It unquestionably would assist you with envisioning the requirement for abstraction:
abstract class Area 
{
   String name = "";

   Area(String num) 
{
      name = num;
   }

   abstract int getArea();
}

class Rectangle extends Area 
{
   int length = 0;
   int width = 0;

   Rectangle(String num, int l, int w) 
      super(num);
      length = l;
      width = w;
   }

   int getArea() 
{
      return length*width;
   }
}


class Circle extends Area 
{
   int radius = 0;

   Circle(String num, int r) 
  {
      super(num);
      radius = r;
   }

   int getArea() 
  {
      return (int) (3.14*radius*radius);
   }
}


public class Main 
{
   public static void main(String args[]) 
   {
      Rectangle obj = new Rectangle("Rectangle", 1, 2); 
      System.out.println( obj.name ); 
      System.out.println( obj.getArea() );

      Circle obj1 = new Circle( "Circle", 2 ); 
      System.out.println( obj1.name ); 
      System.out.println( obj1.getArea() );
   }
}
After running the above Java code, you would see the output as shown below:
Rectangle
2
Circle
12
We trust this instructional exercise on Java abstract class would have assisted you with sorting out how the abstraction functions in Java. Kindly practice with the guides to get full clearness.

Recommended Post:

Source link

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment