Skip to main content

Abstract Class In Java

An abstract class is a class which cannot be instantiated. An abstract class is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass:
  1. Define methods which can be used by the inheriting subclass.
  2. Define abstract methods which the inheriting subclass must implement.
  3. Provide a common interface which allows the subclass to be interchanged with all other subclasses.
Here's an example:
package com.codetalk.abstractclass;

/**
 * @author Rakesh KR
 *
 */
abstract public class AbstractClass {

    abstract public void abstractMethod();

    public void implementedMethod(){

        System.out.println("Implemented Method");

    }

    final void finalMethod(){

        System.out.println("Final Method");

    }

}
Here's a correct ImplementingClass
package com.codetalk.abstractclass;

/**
 * @author Rakesh KR
 *
 */
public class ImplementingClass extends AbstractClass{

    public void abstractMethod() {

        System.out.println("Abastract Method");

    }

    public void implementedMethod(){

        System.out.println("Overridden !!!");

    }

    /*
    public void finalMethod(){

        System.out.println(" Error !!! ");

    }
    */

}
The implementation of finalMethod() in AbstractClass is marked as the final implementation of finalMethod(): no other implementations will be allowed, ever.
Now you can also implement an abstract class twice:
package com.codetalk.abstractclass;

/**
 * @author Rakesh KR
 *
 */
public class SecondImplementingClass extends AbstractClass{

     public void abstractMethod() {

         System.out.println("Second AbstractMethod"); 

     }

}
Now the final class looks likes,
package com.codetalk.abstractclass;

/**
 * @author Rakesh KR
 *
 */
public class MainClass {

    public static void main(String[] args) {

        ImplementingClass       a = new ImplementingClass();
        SecondImplementingClass b = new SecondImplementingClass();
        AbstractClass           c = new ImplementingClass();
        AbstractClass           d = new SecondImplementingClass();

        a.abstractMethod();    // prints "Abstract Method"
        a.implementedMethod(); // prints "Overridden !!!"
        a.finalMethod();       // prints "Final Method"
        b.abstractMethod();    // prints "Second AbstractMethod"
        b.implementedMethod(); // prints "Implemented Method"
        b.finalMethod();       // prints "Final Method"

        c.abstractMethod();    // prints "Abstract Method"
        c.implementedMethod(); // prints "Implemented Method"
        c.finalMethod();       // prints "Final Method"
        d.abstractMethod();    // prints "Second AbstractMethod"
        d.implementedMethod(); // prints "Implemented Method"
        d.finalMethod();       // prints "Final Method"

    }

}
Lastly, you cannot do the following:
public class ImplementingClass extends AbstractClass, SomeOtherAbstractClass {
    ... // implementation
}
Only one class can be extended at a time. If you need to extend multiple classes, they have to be interfaces. You can do this:
public class ImplementingClass extends AbstractClass implements InterfaceA, InterfaceB {
    ... // implementation
}

Comments

Popular posts from this blog

System.out.println In Java

System  is a class in the  java .lang package.  out  is a static member of the  System  class, and is an instance of  java .io.PrintStream .  println  is a method of  java .io.PrintStream . This method is overloaded to print message to output destination, which is typically a console or file. System is a class, that has a public static field out . So it's more like. public final class System { /** * The "standard" output stream. This stream is already * open and ready to accept output data. Typically this stream * corresponds to display output or another output destination * specified by the host environment or user. * <p> * For simple stand-alone Java applications, a typical way to write * a line of output data is: * <blockquote><pre> * System.out.println(data) * </pre></blockquote> * <p> * See the <code>println...

Angular (web framework)

 Angular is a TypeScript-based free and open-source web application framework lead by the Angular Team at Google and by a community of individuals and corporations. Angular is a complete rewrite from the same team that built AngularJS. Google designed Angular as a ground-up rewrite of AngularJS. Angular 14 was released on June 02, 2022. Some new features include typed forms, standalone components, and new primitives in the Angular CDK (component dev kit). All the major releases are supported for 18 months.  This consists of 6 months of active support, during which regularly-scheduled updates and patches are released. It is then followed by 12 months of long-term support (LTS), during which only critical fixes and security patches are released. Official website :- https://angular.io/

ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ ഭയങ്കര പവർഫുൾ ആണ്.

ജാവ യെ  കുറിച്ച്  മലയാളത്തിൽ  ഡോകുമെന്റുകളോ  ബ്ലോഗ്‌കളോ  കൂടുതൽ ഇല്ലാത്തതുകൊണ്ട് എനികറിയവുന്നത് ഷെയർ ചെയ്യാം എന്ന് വിചാരിച്ചു .  ജവയെ കുറിച്ച് പറയുകയാണെങ്കിൽ  ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ  ഭയങ്കര പവർഫുൾ ആണ്. :) 1. നമ്മടെ Sun MicroSystems 1995 ൽ തുടങ്ങി വച്ചാ സംഭവമാണിത് . 2. ഇപ്പൊ ജാവയുടെ എട്ടാമത്തെ വെർഷൻ  വരെ ഇറങ്ങികഴിഞ്ഞു . ജാവയുടെ തിയറിയെ കുറിച്ച് എനിക്ക് പറയാൻ താല്പര്യം  ഇല്ലാ, അത് അറിവിള്ളവർ നല്ലപോലെ പറഞ്ഞിടുണ്ട്. നമുക്ക് ഡയറക്റ്റ്  ആയി കോഡിംഗ് പാർട്ടിലേക്ക്  പോകാം. എങ്ങനെയാ ജാവ ഇൻസ്റ്റോൾ ചെയണ്ടേ എന്നൊക്കെ ഒന്ന് ഗൂഗിൾനോട് ചോദിച്ചാ മതി പുള്ളി പറഞ്ഞുതരും. പിന്നെ എല്ലാവരും പറയുന്നു ജാവാ Object  Oriented ആണെന്ന് . അതെ ജാവാ Object Oriented ആണ് . എന്താണ് Object ?, Object  നു വച്ചാ ഒരു സാധനം അത്രതന്നെ. ഇപ്പൊ example പറയുകയാണെങ്കിൽ Book ഒരു സാധനമാണ് . Book റ്റെ പ്രതേകത എന്തൊക്കെ അന്നെന്നുവച്ചാ ആതിനൊരു Auther ഉണ്ടാകും, അതൊരു Category ൽ പെട്ട book ആയിരിക്കും (Novel / Academic ) പിന്നെ ഓരോ bookനും അ...