Below are the some question which are generally asked in Java interview question from Abstract class and interface.
Question-Can abstract class have constructor?
Yes,An abstract class can contain a constructor. An abstract class constructor is used to initialize fields of the abstract class. You could provide a constructor for an abstract class if you want to initialize specific fields of the abstract class before the instantiation of a child-class.We cannot create an instance of an abstract class, But we can create instances of classes those are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor will automatically get called.
Question-Can abstract class implements interface in Java? Is it necessary to implement all methods?
Yes, abstract class can implement the interface. When we implement an interface to an abstract class, its means that the abstract class inherited all the methods of the interface.
Its depend upon the class which is implementing the interface. When we implement an interface to an abstract class, then the abstract class inherited all the methods of the interface. As, it is not important to implement all the method in abstract class however it comes to abstract class , so the abstract class can left some of the method in interface without implementation. In concrete class we need to implement all the inherited method.
Question-Can we declare abstract class as final in Java?
No a class cannot be declare as "abstract" and "final" both. Declaring a class as "abstract" means it contains method definition and hence depends on its subclasses to provide concrete implementation.
Making a class "final" means that no other class can extend it and hence abstract classes cannot be marked final.
In short An abstract class must be sub-classed, whereas a final class must not be sub-classed.
An abstract class without being inherited is of no use and hence will result in compile time error.
Question-Can an abstract class have static methods in Java?
Yes, an abstract class can have static method. You cannot create an abstract static method, you can create a non abstract static method.
Reason is you do not need a object instance to access a static method because that method can be called directly, even if you do not have an instance of the abstract class.(to invoke static method you do not need to build a class instance (i.e object))
Question-is it possible to create a instance of abstract class in java?
No, its never possible to instantiate an abstract class. But might be interviewer can say “YES WE CAN” at that time as below,
abstract class myClass {
public void m1()
{System.out.print(“Inside Abstract class");}}
class implmentmyClass { public static void main(String a[]) { myClass m = new myClass () {};
m.m1(); }
}
The given code block in last page instantiates an anonymous inner class which is a subclass of the myClass abstract class. It is not same as instantiating the abstract class itself. we can use new keyword to create an anonymous class instance by just adding {} as implement body at the end of the abstract class. so its not the abstract class instantiation its anonymous class.
Question- Is it necessary for abstract class to have abstract method?
No, we can have an abstract class without Abstract Methods as both are independent. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass. So you can easily define an abstract class without any abstract method.
Question- Can we define main method in abstract class?
Yes, we can define main method inside the abstract class, Loading a class is not the same as creating an instance/object of the class(using new operator). And there's no need to create an instance of the class to call main(), because it's static method. So there's no problem if we will define the main method inside an abstract class.
Question- Can you use abstract and final both with a method.
No, we can not declare abstract method as final. We have to provide implementation to abstract methods in subclasses, and if we will mark it as final then we can’t provide the method reimplementation for that final method in it’s derived classes.
final abstract int add(int n1,int n2);
Here we can’t provide the implementation for the add method as it is declared as final. But at the same time you declare it as abstract. Here we can’t provide the implementation for the add method. Even we described to provide implementation but we cant.
Question- Can you declare an interface method as static ?
NO, interface only contains abstract method.
Suppose we have a Test class with a static method add. we can call this as
Test.add(2, 3);
If Test were an interface instead of a class, it could not have any defined functions. As such, calling something like Test.add(2, 3) doesn't make any sense.
Question- Can we declare an Interface be final ?
No. We can not instantiate interfaces, so in order to make interfaces useful we must create subclasses. When the final keyword appears in a class declaration, it means that the class may never be subclassed or overridden. Interface represent behaviour, rather than implementation, therefore it makes no sense for it to be final.
Question- Can we define private and protected modifiers for variables in interfaces.
No we cant define the variable with private, protected in interface. only public, static & final are permitted with variable inside an interface. An interface provide a way for the client to interact with the object. If variables will not be public, the clients would not have access to them. that is why variable are public.
Question-Can we use “abstract” keyword with constructor?
No, we cant declare the constructor as abstract because abstract means we must have to override it in non abstract child class and you can not override a constructor. Constructors are not inherited thus we cannot override them.
Question-Can we declare abstract methods as private?
No. Abstract methods can not be private. we cant inherit the private method, so its doesn't make a sense to declare a private method abstract. Declaring a abstract method means we need to override and implement it in child/subclass, but we cant override private methods so we cant make it abstract. You can declare it protected instead of private.
Question-We can’t instantiate an abstract class. Then why constructors are allowed in abstract class?
The use of constructor to initialize the instance of classes. We use a constructor to create new object often with parameter specifying the initial value or other important information about the object. Suppose inside a class we have some fields we always want them to initialize, to do this we create a constructor and initialize them. If we have two different subclasses of abstract class when we instantiate them their constructor will get called and then parent constructor will get called and field will get initialized. Every constructor invokes the constructor of its super class with an implicit call to super.
Question-Abstract classes can be nested. True or false?
True, Abstract classes can be nested. A abstract nested classes are the inner classes marked with abstract modifier. Inner class acts as a member of the enclosing class and can have any access modifiers: abstract, final, public, protected, private, static. For more details please dig the Inner Class in java.
Question-Can we declare abstract methods as synchronized?
Yes we can declare the abstract method as synchronized but it would be useless to do so since synchronization is an aspect of the implementation, not the declaration, and abstract methods do not have an implementation. You can use the 'synchronized' modifier on a concrete implementation of an abstract method, which is where it belongs.
Question-Can abstract method declaration include throws clause?
Yes. We can define an abstract class with throws clause.
Question-What are the valid and invalid keywords or modifier with abstract class?
Valid - public, protected and default.
Invalid - static, final and private.
No comments:
Post a Comment