Java abstract

Abstract method 

An abstract method is a method without method body. That means it does not contain any body. It contains only the method header. So we can say it is an incomplete method.

An abstract method is written when the same method has to perform different tasks depending on the object calling it.

Abstract class

An abstract class is a class that generally contains 0 or more abstract method. Both the abstract class and the abstract methods should be declared by using the keyword ‘abstract’.

Program of abstract class and abstract method
abstract class bike
{
bike()
{
System.out.println(“It is bike”);
}
abstract void nongear();
void gear()
{
System.out.println(“It is gear”);
}
}
class car extends bike
{
void nongear()
{
System.out.println(“It is nongear”);
}
}
class test
{
public static void main(String args[])
{
bike obj = new car();
obj.nongear();
obj.gear();
}
}

Output:
It is bike
It is nongear
It is gear

Write a program to find addition of given number using abstract
abstract class Sum
{
public abstract int SumOfTwo(int n1, int n2);
public abstract int SumOfThree(int n1, int n2, int n3);
public void disp()
{
System.out.println(“This is Basicofy program”);
}
}
class test extends Sum
{
public int SumOfTwo(int n1, int n2)
{
return n1+n2;
}
public int SumOfThree(int n1, int n2, int n3)
{
return n1+n2+n3;
}
public static void main(String args[])
{
test obj = new test();
System.out.println(obj.SumOfTwo(10,20));
System.out.println(obj.SumOfThree(10,20,30));
obj.disp();
}
}

Output:
30
60
This is Basicofy program

Leave a Reply

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