The use of abstract method & class in java- with example - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline

The use of abstract method & class in java- with example

 The use of abstract method & class in java



What is abstract method?

  As abstract method is a method without method body. An abstract is written
  when 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);
   }
}





About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.