Abstract Class in Java

Abstract Class in Java

Before perusing this instructional exercise I will prescribe you to peruse

about unique binding in java, on the grounds, that without thinking about powerful binding

you can’t comprehend the idea of abstract class in java.

Abstraction in Java

  • Abstraction is the way toward concealing the multifaceted nature and demonstrating the usefulness. Demonstrating the fundamental information and covering up to no basic information. We can comprehend this by taking a certifiable model.
  • We have a fan in the house, we are simply furnished with a switch (usefulness) to turn on and off the fan, we don’t know about how the fan really functions (intricacy). This is an ideal case of abstraction in java.

Abstraction in java is accomplished in two different ways:

  1. Abstract Class (Partial usage of abstraction)
  2. Interface (Fully usage of abstraction)

Abstract Class in Java

  • A class which has at least zero abstract strategies is known as abstract class. It can likewise have non-abstract strategies.
  • Abstract class can’t be launched. It implies we can’t make an object of an abstract class. Any way we can make its reference variable.

Abstract Method in Java

The abstract strategy is a technique without a body. Abstract class and

abstract strategy is announced utilizing “abstract” catchphrase. The following is a model.

abstract class Demo        //abstract class
 
{
 
abstract void add(int a, int b);     //abstract method
 
. . . . .
 
. . . . .
 
}

At the point when we get a class from an abstract class then we have

to give the body of all the abstract techniques in the inferred class. On the off chance that we don’t give

an assortment of techniques then we need to make the inferred class as abstract class.

The following is one program model that actualizes the idea of

abstract class in java.

Abstract Class Example

abstract class Base
 
{
 
                abstract int calculate(int a);
 
               
 
                void display()
 
                {
 
                                System.out.println("Base");
 
                }             
 
}
 
 
class Square extends Base
 
{
 
                int calculate(int a)
 
                {
 
                                return a*a;
 
                }
 
}
 
 
class Cube extends Base
 
{
 
                int calculate(int a)
 
                {
 
                                return a*a*a;
 
                }
 
}
 
 
class Demo
 
{
 
 
                public static void main(String...s)
 
                {
 
                                Base b1=new Square();
 
                                Base b2=new Cube();
 
                               
 
                                System.out.println(b1.calculate(2));        //will call calculate() of Square class
 
                                System.out.println(b2.calculate(2));        //will call calculate() of Cube class
 
                                b1.display();       //will call method of Base class
 
                }
 
}

Output

Abstract Class in Java

In the above model, the task is performed relying on the

object calling it. As I have just disclosed to you that abstract class can’t be

started up. So in the above program we are simply announcing a reference variable of

abstract class Base and putting away the reference id of inferred class in it, this

idea is called as powerful binding.

Let’s talk around barely any significant inquiries for an abstract class in java

Inquiries Questions

Q. Why we utilize dynamic binding?

A. To accomplish abstraction.

Q. How we execute parent class strategies?

A. Utilizing dynamic binding. This is actualized in the above program.

Q. Would we be able to keep developing in abstract class?

A. Each class in java have constructor so abstract class additionally have a constructor.

Q. Would we be able to make abstract technique static?

A. No, in light of the fact that static is gotten to by means of the class name and it will be gotten to by anybody.

This was about abstract class in java. In the event that you discover anything

missing, off base or have questions in regards to above instructional

exercise at that point please notice it by remarking beneath.

Leave a Comment

error: Alert: Content is protected!!