OOPM JAVA - Create a abstract class Abstract Sum which has abstract methods sumofTwo() and sumofThree()

 Experiment No. 12

Aim: Create a abstract class Abstract Sum which has abstract methods sumofTwo() and sumofThree().Create a class Sum which extends AbstractSum and implement it’s methods.

Objective: To make aware of Abstract class and abstract methods concept. 

Outcome: Students implemented Abstract class and abstract methods.

Theory:

A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods (methods without body) as well as concrete methods (regular methods with body). A normal class (non-abstract class) cannot have abstract methods. An abstract class cannot be instantiated, which means you are not allowed to create an object of it. 

Abstract class Example

//abstract parent class

abstract class Animal{

   //abstract method

   public abstract void sound();

}

//Dog class extends Animal class

public class Dog extends Animal{

public void sound(){

    System.out.println("Woof");

   }

   public static void main(String args[]){

    Animal obj = new Dog();

    obj.sound();

   }

}

An abstract class outlines the methods but not necessarily implements all the methods.

//Declaration using abstract keyword

abstract class A{

   //This is abstract method

   abstract void myMethod();

   //This is concrete method with body

   void anotherMethod(){

      //Does something

   }

}

A class derived from the abstract class must implement all those methods that are declared as abstract in the parent class. Abstract class cannot be instantiated which means you cannot create the object of it. To use this class, you need to create another class that extends this this class and provides the implementation of abstract methods, then you can use the object of that child class to call non-abstract methods of parent class as well as implemented methods (those that were abstract in parent but implemented in child class). If a child does not implement all the abstract methods of abstract parent class, then the child class must need to be declared abstract as well.

 

Example of Abstract class and method

abstract class MyClass{

   public void disp(){

     System.out.println("Concrete method of parent class");

   }

   abstract public void disp2();

}

class Demo extends MyClass{

   /* Must Override this method while extending

    * MyClas

    */

   public void disp2()

   {

       System.out.println("overriding abstract method");

   }

   public static void main(String args[]){

       Demo obj = new Demo();

       obj.disp2();

   }

}


Algorithm:

  1. Start
  2. Create a abstract class AbstractSum.
  3. Class AbstractSum has abstract methods sumofTwo() and sumofThree().
  4. Create a class Sum.
  5. Class Sum extends AbstractSum and implement it’s methods 
  6. Stop.


Code :

abstract class AbstractSum

{

   public abstract int sumOfTwo(int num1, int num2);

   public abstract int sumOfThree(int num1, int num2, int num3);

   public void disp()

   {

  System.out.println("Sum of Numbers");

   }

}

class Sum extends AbstractSum

{

   public int sumOfTwo(int num1, int num2)

   {

  return num1+num2;

   }

   public int sumOfThree(int num1, int num2, int num3)

   {

  return num1+num2+num3;

   }

   public static void main(String args[])

   {

  AbstractSum obj = new Sum();

  obj.disp();

  System.out.println("Sum of Two : "+obj.sumOfTwo(10, 21));

  System.out.println("Sum of three : "+obj.sumOfThree(7, 11, 9));

   }

}


Output : 

Create a abstract class Abstract Sum which has abstract methods sumofTwo() and sumofThree()


Observations and learning: 

In this experiment we have learnt about the concept of abstract class and abstract methods.

A class derived from the abstract class must implement all those methods that are declared as abstract in the parent class. Abstract class cannot be instantiated which means you cannot create the object of it.

 

Conclusion: 

In this experiment we used abstract class and abstract methods to get the desired output.

We calculated sum of two numbers and sum of three numbers using this program.

 

Question of Curiosity 

1. Can abstract class have constructors in Java?

Ans : Yes, an abstract class can have a constructor in Java. You can either explicitly provide a constructor to abstract class or if you don't, the compiler will add default constructor of no argument in abstract class.

 

2. Can abstract class implements interface in Java? Does they require to implement all methods?

Ans : In Java, an abstract class can implement an interface, and not provide implementations of all of the interface's methods.

 

3. Can you create instance of abstract class?

Ans : A Java abstract class is a class which cannot be instantiated, meaning you cannot create new instances of an abstract class. 

 

4. Is it necessary for abstract class to have abstract method?

Ans : It's not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn't declare any abstract methods.

 

5. Can abstract class contains main method in Java?

Ans : Yes, you can use the main method in abstract class. The main method is a static method so it is associated with Class, not with object/ instance.


6. Compare Abstract Class and Interface.

Abstract class

Interface

  1. An abstract class can extend only one class or one abstract class at a time.

  2.  An abstract class can extend another concrete class or abstract class.

  3. An abstract class can have both abstract and concrete methods.

  4. An abstract class can have protected and public abstract methods.

  1. An interface can extend any number of interfaces at a time.


  1. An interface can only extend another interface.

  2. An interface can have only abstract methods.

  3. An interface can have only have public abstract methods.

Comments