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
Syntax of Abstract Classabstract class <name> { Abstract return function(); }
Example of Abstract Class
Input Value
Example of Abstract Classabstract 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(); } }
Let’s run the code
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
Example of Encapsulation in Java
Input Value
Example of Encapsulation in Javaclass 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(); } }
Let’s run the code
The 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
0 Comments