HTML Java

Java Abstraction Interview Questions


Ques: Why we can’t have static abstract method together?

Sol: Because static method is associate with class static binding and abstract associated with object and its dynamic binding.


Ques: Why abstract private not together?

Sol: Because we can take it out the private method out of the class and we have to override abstract method outside the class so thatswhy.


Ques: Why abstract final not together ?

Sol: Because final method can’t be override.


Ques: Why we can’t create an object of abstract class?

Sol: Because what we will we do with that object because inside abstract class only method declaration is there so even though we cant use that method thats why.


Ques: What is the difference between Abstract class and interface?

Sol:

And interface represent only behavior of object and we can’t create of object of an interface. Inside interface no init block no static block no constructor to use interface we write implements and interface name which class going to Use this interface and to use abstract class we use extends. Through interface we achieve multiple inheritance but not with abstract class.


Ques: When we use interface/abstract class?

Sol: When we have requirement specification we should go with interface and when we know requirement and as well as some implementation but not 100% then we should go for abstract.

Example

public abstract class Birds {
   //abstract String color="blue"; abstract datamember not allowed
   String color ="blue";
   abstract void eat();
   abstract void drink();
   void fly() {
     System.out.println(" fly");
   }
   Birds() {
     // constructor
   }
   // we can have main method in this abstract class also
}

// abstract class Crow extends Birds
//{} if u don't wanna to override abstract method in crow then make it abstract
// if u wanna override then override all of them in crow not single every method

class Parrot extends Birds {
   void eat() {
     System.out.println(" Parrot eats");
   }

   void drink() {
     System.out.println(" Parrot drinks");
   }
   public static void main(String[] args) {
     //Birds b =new Birds(); we can't create an object of abstract class
     Birds b =new Parrot(); //reference of abstract class and object of child class Crow

     b.eat();
     b.drink();
     b.fly();
   }
}
Try it »

Dynamic object Creation using Class.forName

Example

abstract class MasterRemote {
   abstract void changeTemperature();
   abstract void modeChange();
}

class Samsung extends MasterRemote {
   void changeTemperature() {
     System.out.println("temperature of Samsung AC is changed");
   }

   void modeChange() {
     System.out.println("Mode of Samsung AC is changed");
   }
}
class Voltas extends MasterRemote {
   void changeTemperature() {
     System.out.println("temperature of Voltas AC is changed");
   }

   void modeChange() {
     System.out.println("Mode of Voltas AC is changed");
   }
}
class OperateAC {
   public static void main(String s[]) {
     try {
       Class c= Class.forName(s[0]);
       MasterRemote mr=(MasterRemote)c.newInstance();
       mr.modeChange();
       mr.changeTemperature();
     }catch(Exception e) {
       System.out.println("Enter the name of class to load");
     }
   }
}
Try it »