Ans: An Abstract Class is class which is incomplete, that means it has some methods/functions are undefined and some/functions are fully defined.
Example : Below is the Abstract class
Abstract class Program
{
public abstract void undefinedFunction();
void definedFunction()
{
Syso("prints Sekhar");
}
}
Key Points : 1) use must the Keyword "Abstract " before class name to define that class as a Abstract.
2)We cant Create the Objects of Abstract class using the new Operator.
3)An Abstract class Extends other class,if it is not extending the other class, then like any
other class it extends the Object class.
4)Any function definition inside the Abstract class without a body would be Default Public.
5) We can define an Abstract class without abstract function definition inside it.Hence
defining abstract function definitions inside an Abstract class is not Compulsory.
Example of 5th Point :
Abstract class Program
{
public void function()
{
Syso("An Abstract class without an Abstract function");
}
}6) we can define an Abstract class only with Abstract functions inside it.
Note : If an Class Extends the abstract class than that class should provide bodies for all the abstract functions defined inside and Abstract otherwise the child class should be defined as Abstract class.
Otherwise we wil get an Compilation Error.
Example :
Abstract class Parent
{
abstract void function1();
abstract void function2();
}
Abstract class child extends Parent
{
public void function1()
{
Syso("Providing the function1 Body for the Class parent");
}
public void childFunction()
{
Syso("This is My Own CLass");
}
}
7) we cannot create an Object for the Abstract Class , but we can Create a Reference for the Abstract class and Assign the Object of the class which extends the Abstract class.
8) Abstract class can even Extends the any other Abstract class.
9) Abstract class can even Implements the Interface.
10) An Abstract Class can Implement an Interface by not Providing the bodies for those functions inside the Interface.
11) We no Need to Provide body for the functions which are not Abstract in the Abstract Class.
12) Abstract can have Constructors, and this Constructor will get called once we create the Object for the Extended Class.
Scenarios where Constructor of the Abstract Class Will get Called.
Scenario 1 : ExtendedClass ec = new ExtendedClass();
Scenario 2 : AbstractClass ac = new ExtendedClass();
13) Abstract functions cannot be static in Abstract Class.
A Sample Abstract Class:
public abstract class AbstractClass {
public AbstractClass(){
System.out.println(" In Constructor");
}
public void functionX() {
System.out.println("Function X");
}
public void functionY(){
System.out.println("Function Y");
}
public void functionA(){
System.out.println(" Function A of Super Type Defined here");
}
public void functionB(){
System.out.println(" Function B of Super Type Defined here");
}
public static void staticFunction1(){
System.out.println(" Static Function");
}
}
No comments:
Post a Comment