The use of abstract method & class in java
What is abstract method?
As abstract method is a method without method body. An abstract is writtenwhen the same method has to perform different tasks depending on the object calling
it.
* abstract class most be declear a abstract keyword.
* abstract method does not contain any body.
* It contains the only the method header.
What is abstract class?
An abstract class is a class that contains 0 or more abstract methods.* An abstract class is a class that generally contains some abstract
method.
* the abstract class and abstract method should be declared by using the key
word 'abstract'.
Note.
Abstract class can have datamember abstract method , method body constructor every
main() method.
Abstract class has one abstract method which has got various implementaions in sub classes such as (A,B,,C).
abstract class Myclass
{
abstract void calculate (double x);
}
class A extends Myclass
{
void calculate(double x)
{
System.out.println("Square="+(x*x));
}
}
class B extends Myclass
{
void calculate (double x)
{
System.out.println("Square root="+Math.sqrt(x));
}
}
class C extends Myclass
{
void calculate(double x)
{
System.out.println("Cube="+(x*x*x));
}
}
class AbcMainClass
{
public static void main(String args[])
{
// create A,B,C class objects
A a=new A();
B b= new B();
C c=new C();
// call the calculate() method
a.calculate(5);
b.calculate(6);
c.calculate(7);
}
}
0 comments:
Post a Comment