In this tutorial, we will learn about the Abstract Class in Java or Encapsulation in java. Its types, and how to use them with the help of examples.

Define Abstract Class in Java

Abstract Class is a kind of class. Abstract class is the partial template class whereas some methods are in define from and some are declare form. The abstract class contains both normal method and abstract method . The abstract class can be define using abstract keyword. This class has no individual meaning means we cannot create the object of the abstract class but we can use it by inherit with some other class. Abstract class has at least one abstract method.

The Abstract Method

Abstract method is the method declare using “abstract” keyword within abstract class. It means abstract method has only prototype within abstract class but it can be define in those class who inherit it.

Syntax of Abstract Class

abstract class <name>
		{
		Abstract return function();
		}
Syntax of Abstract Class

Example of Abstract Class

Input Value

abstract class abms {

    public abstract void book();
    public void favbook() {
      System.out.println("maths book");
    }
  }
  
  class self extends abms {
    public void book() {
    
      System.out.println("My fav is a : maths book");
    }
  }
  
  class Main {
    public static void main(String[] args) {
      self mybook = new self(); 
      mybook.book();
      mybook.favbook();
    }
  }
  
Example of Abstract Class

Let’s run the code

Input Value

Output Value

Output Value

Encapsulation in Java

Encapsulation is a process of wrapping code and data together into a single unit, it’s know as Encapsulation in Java. Java Bean class is the example of a encapsulated class.

Real life example of Encapsulation

Capsule
Capsule

Example of Encapsulation in Java

Input Value

class Area {

    int length;
    int breadth;

    Area(int length, int breadth) {
        this.length = length;
        this.breadth = breadth;
    }

    public void getArea() {
        int area = length * breadth;
        System.out.println("Area: " + area);
    }
    }
    
    class abms {
    public static void main(String[] args) {
    
        Area rectangle = new Area(16, 9);
        rectangle.getArea();
    }
    }
    
Example of Encapsulation in Java

Let’s run the code

Input Value

The Output Value

Output Value

Super Keyword in Java

This keyword is use for passing the parameters value to the base class constructor from derived class constructor. It should be make sure that super keyword must be used as first line statement of derived constructor.

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *