http://www.technicalpage.net/search/label/SQL

>> Abstract Class

Abstract Class in Java


A class declared using the keyword "abstract" is called abstract class. It may be having one or more or no abstract methods inside it. An abstract class can have abstract methods and/or non-abstract methods and/or static fields/methods and/or constructors and/or final fields/methods.

An abstract class can not be instantiated but you can use inheritance into it , ie , subclassed. The abstract methods inside the abstract class are the methods without implementation or without body or also called undefined mehtods. When the subClass inherites(extends) the abstract class , the subClass implements or has to implement all the abstract mehtods in the parent abstract class. However, if the child class is also an abstract class then it does not need to implement all the abstract methods from the parent class or if the sub class does not implements all the abstract methods from the parent class, then the child class needs to be converted to abstract class. And in such situation you can not instantiate the child class as well. 

The default constructor in parent class and/or child class are automatically called when the child class is instantiated.

Abstract Class can have private method only when the method is non-abstract.

The abstract class can have methods with different access modifiers such as public , protected, private. When inherited by the child class, the methods can be changed to better visibility access modifiers, such as protected is changed to public.

When all the abstract methods inside an abstract(parent) class are implemented by the child/sub class then the child class can be instantiated.

Abstraction is a process to maintain privacy of information. It hides the implementation ,ie, it only shows the functionality, ie, it shows what to do and does not show how to do. The functionality or implementation is defined by the class which implements/inherits the abstract class(abstract methods) .

Examples:

Parent abstract class



public abstract class Parent {



       public abstract void red();

       protected abstract void blue();
      
       //private abstract void white(); //private abstract method is not supported.
      
       private void white1() {  //private non-abstract method is supported.
       }
      
       public static void brown() { //static methods are supported inside abstract class.
              System.out.println("Static method.");
       }
      
       public Parent() {  // constructor is allowed inside abstract class.
              System.out.println("Parent Constructor.");
       }
      
       //Variables
       int x ;
       int y =10;
       public String name = "Hari" ;
       private String id ;
       public static int number = 15;
       final int xyz = 100;
      
}


Child non-abstract class

public class Child extends Parent {

       public void red() {
              System.out.println("First abstract method.");
       }
      
       public void blue() { //visibility was protected in parent class, changed to public
             
              System.out.println("Second abstract method.");
       }
      
       public Child() {  //Child Constructor
              System.out.println("Child Constructor.");
       }
      
       public static void main (String[] Args) {
              Parent.brown(); // calling static method without creating object.
              Child.brown(); // or this also calls same method.
              System.out.println("Global from parent = "+Parent.number);// calling static field.
              System.out.println("Global from child = "+Child.number);// or this also calls same field.
              Child ch = new Child(); // creating object, this will automatically call the constructors.
              ch.red();
              ch.blue();
              ch.brown(); //but to call static method, no need to create object , as called above.
//            ch.(any variables from parent or child class.)

             
             
       }


}

Output:
Static method.
Static method.
Global from parent = 15
Global from child = 15
Parent Constructor.
Child Constructor.
First abstract method.
Second abstract method.
Static method.

No comments:

Post a Comment