PHP Core / OOPs / QA
Oops QA
-
Abstract Class
1. How abstract method works with the final keyword?
An abstract method can’t declare as final.
Reasonabstract and final terms are opposite to each other. An abstract method says you must redefine it in a derived class. A final method says you can’t redefine this method in a derived class. Let’s see how the use of abstract methods in java.
2. How abstract method works with the private keyword?
An abstract method can’t be private because a private method is not accessible outside the class.
Reason:An abstract method says programmers have to provide the implementation in the derived class. The programmer provides implementation only if it is accessible from outside the class. A private method is not accessible from outside the class. So, we can’t use the private keyword with abstract methods.
3. How abstract method works with the static keyword?
An abstract method can’t be static because we can’t override a static method.
ReasonIf Java allows us to create an abstract static method(Method without body) and someone calls the abstract method using the class name(because the static method can access directly by class name), then What would happen? Because it will be an incomplete method.
4. Why can’t we create the object of an abstract class?
Abstract classes are not complete because they have abstract methods and abstract methods don’t have a body. If java allows the creation of an object of the abstract class, then it will lead to two problems:
5. How to use abstract classes and abstract methods in Real life?
Let’s say, I am the principal of a college and I want details of each department of the college. My college has various departments like Art, science, commerce, etc. I want to know the department name, how many teachers are in each department(mandatory information), and some additional information. So, I will create a structure that will be followed by each department. I will create an abstract class with an abstract method that will be inherited by each department. So each department must provide the implementation of the abstract method.
Here we have three classes one class is abstract (Department) and the other two are concrete classes(Art class and science class). These concrete classes inherit the abstract class so they must provide the implementation of abstract methods.
6. Can an abstract class be final
No, An abstract class can’t be final because the final and abstract are opposite terms in JAVA.
ReasonAn abstract class must be inherited by any derived class because a derived class is responsible to provide the implementation of abstract methods of an abstract class. But on another hand, if a class is a final class, then it can’t be extended(inherited). So both concepts are opposite to each other.
6. Can we create an instance of an abstract class?
No, we can’t create an object of it. As know abstract class is not a complete class. because an abstract class has abstract method (Methods without body). Although an abstract class has a constructor if you will try to create an object of it, It will throw compile time exception.
7. Can an abstract class have a constructor?
An abstract class can have abstract methods and we can’t create its object. You can read “Why we can’t create an object of the abstract class?” But here a question strike in mind, Can abstract class have a constructor? If yes, then why and what is the use of constructor in abstract even we can’t create the object of an abstract class. Here we will discuss it in detail.
Yes, an abstract class can have a constructor in Java. The compiler automatically adds the default constructor in every class either it is an abstract class or concrete class. You can also provide a constructor to abstract class explicitly. The constructor is always used to initiate a class or use it to create an object of the class. But we can’t instantiate it with the new() operator or any other ways.