Answers for "what is abstract class"

12

what is abstract class

when we use abstract classes? supposably we have class Car. 
you don't want to use it directly. you need extend this class. 
thats why you must declare this class as abstract. 
then you only can extend this class with all methods.
Posted by: Guest on October-16-2021
45

abstract class in java

Sometimes we may come across a situation where we cannot provide 
implementation to all the methods in a class. We want to leave the 
implementation to a class that extends it. In such case we declare a class
as abstract.To make a class abstract we use key word abstract. 
Any class that contains one or more abstract methods is declared as abstract. 
If we don’t declare class as abstract which contains abstract methods we get 
compile time error.
  
  1)Abstract classes cannot be instantiated
  2)An abstarct classes contains abstract method, concrete methods or both.
  3)Any class which extends abstarct class must override all methods of abstract
    class
  4)An abstarct class can contain either 0 or more abstract method.
Posted by: Guest on November-30-2020
6

abstraction in java

Abstraction is defined as hiding internal implementation and showing only 
necessary information.
// abstract class
abstract class Addition
{
   // abstract methods
   public abstract int addTwoNumbers(int number1, int number2);
   public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
   // non-abstract method
   public void printValues()
   {
      System.out.println("abstract class printValues() method");
   }
}
class AbstractMethodExample extends Addition
{
   public int addTwoNumbers(int number1, int number2)
   {
      return number1 + number2;
   }
   public int addFourNumbers(int number1, int number2, int number3, int number4)
   {
      return number1 + number2 + number3 + number4;
   }
   public static void main(String[] args)
   {
      Addition add = new AbstractMethodExample();
      System.out.println(add.addTwoNumbers(6, 6));
      System.out.println(add.addFourNumbers(8, 8, 3, 2));
      add.printValues();
   }
}
Posted by: Guest on October-23-2020
2

is it necessary for abstract class to have abstract method

Abstract classes CAN have non-abstract methods.
Posted by: Guest on August-27-2021
5

is it necessary for abstract class to have abstract method

No, abstract class can have zero abstract methods.
Posted by: Guest on November-28-2020
1

how to use an abstract class in java

interface methods{
    public void hey();
    public void bye();
}

//unable to implement all the abstract methods in the interface so 
// the other class automatically becomes abstract
abstract class other implements methods{
    public void hey(){
        System.out.println("Hey");
    }
}

//able to implement all the methods so is not abstract
class scratch implements methods {
    public void hey(){
        System.out.println("Hey");
    }
    public void bye() {
        System.out.println("Hey");
    }
}
Posted by: Guest on January-08-2020

Code answers related to "what is abstract class"

Browse Popular Code Answers by Language