Thursday, December 12, 2019

Can Abstract Bird Convey Constructor Inward Coffee - Interview Question

Yes, an abstract bird tin possess got a constructor inwards Java. You tin either explicitly render a constructor to abstract bird or if y'all don't, the compiler volition add together default constructor of no declaration inwards abstract class. This is truthful for all classes in addition to it too applies to an abstract class. For those who desire to recollect what is an abstract bird inwards Java, it's a bird which tin non endure instantiated amongst new() operator or whatever other ways. In guild to purpose an abstract bird inwards Java,  You involve to extend it in addition to render a concrete class. Abstract bird is usually used to define a base of operations bird for a type hierarchy amongst default implementation, which is applicable to all modest classes. By the way, difference betwixt interface in addition to abstract bird inwards Java is too i of the pop in addition to tricky Java questions in addition to should endure prepared good for Java interviews.  


Can nosotros declare constructor on abstract bird inwards Java is follow-up of other  similar Java interview questions e.g. Can nosotros override static method inwards Java?. Why does interviewer inquire this questions? Mainly because, trying to confuse programmer amongst the fact that since an abstract bird tin non endure instantiated, why abstract bird needs constructor. In this Java Interview interrogation article nosotros volition run across that Abstract bird tin possess got a constructor inwards Java.



Why tin an abstract bird possess got a constructor inwards Java?

Now if nosotros say nosotros tin non practise an instance of an abstract bird in addition to then why practise Java adds a constructor inwards the abstract class. One of the reasons which brand feel is  when whatever bird extends an abstract class, the constructor of sub bird volition invoke the constructor of super bird either implicitly or explicitly. This chaining of constructors is i of the reasons abstract bird tin possess got constructors inwards Java.



Here is an instance Java program, which proves that abstract bird tin possess got constructors inwards Java :

/**  * Simple Java programme to examine that abstract bird tin possess got constructor inwards Java.  * @author http://java67.blogspot.com  */ public class AbstractConstructorTest {      public static void main(String args[]) {        Server server = new Tomcat("Apache Tomcat");        server.start();     } }  abstract class Server{     protected final String name;         public Server(String name){         this.name = name;     }         public abstract boolean start(); }  class Tomcat extends Server{         public Tomcat(String name){         super(name);     }      @Override     public boolean start() {        System.out.println( this.name + " started successfully");        return true;     }     }  Output: Apache Tomcat started successfully  


In this instance Java program, nosotros possess got an abstract bird Server, which has a constructor amongst i parameter, which accepts the name. Subclass provides that advert to superclass piece creating a concrete instance of Server in addition to overriding abstract method starts(). Since this programme compiles in addition to run fine y'all tin definitely say abstract bird tin possess got constructors inwards Java.

 an abstract bird tin possess got a constructor inwards Java Can abstract bird possess got Constructor inwards Java - Interview Question


Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
Can nosotros override a somebody method inwards Java?
  • Difference betwixt overloading in addition to overriding inwards Java
  • Difference betwixt TreeSet in addition to TreeMap inwards Java
  • Top 10 Java pattern pattern interview questions
  • Top 10 tough Core Java Interview questions amongst answers
  • No comments:

    Post a Comment