Abstract Class In Java Programming


Abstract Class: We aware about few thing, what to do, and how to do, where as we know few thing what do to but don't now how to do.

Must watch video:

   

e.g:

Abstract class..

public abstract class NameOfAbstraceClass {
}

Abstract class with one abstract method:

packagecom.tutorialbyexample;

public abstract class Calculator {

       public Calculator() {
              // TODO Auto-generated constructor stub
       }

       abstract void print();
}

Abstract class with one abstract method and one non abstract method:

packagecom.tutorialbyexample;

public abstract class Calculator {

       public Calculator() {
              // TODO Auto-generated constructor stub
       }

       abstract void print();

       public int Addition(int parameter1, int parameter2) {
              return parameter1 + parameter2;

       }

}


Abstract class implemented in SimpleCalculator class by extends keywords:

packagecom.tutorialbyexample;

public class SimpleCalculator extends Calculator {

       public SimpleCalculator() {
              // TODO Auto-generated constructor stub
       }

       public static void main(String[] args) {
              // TODO Auto-generated method stub

       }

       @Override
       void print() {
              System.out.println("Print implementation from abstract class");

       }
}

Object creation of SimpleCalculator class and called both abstract and non abstract methods from abstract class:

packagecom.tutorialbyexample;

public class SimpleCalculator extends Calculator {

       public SimpleCalculator() {
              // TODO Auto-generated constructor stub
       }

       @Override
       void print() {
              System.out.println("Print implementation from abstract class");
       }

       public static void main(String arg[]) {

              SimpleCalculator obj = new SimpleCalculator();
              obj.Addition(1, 2);
              obj.print();

       }
}

Interface for Abstract class:

packagecom.tutorialbyexample;

public interfaceInterfaceForAbstractClass {

       void display();

}

Interface implemented in abstract class Calculator abstract class and display method not required to implement in it:

package com.tutorialbyexample;

public abstract class Calculator implementsInterfaceForAbstractClass {

       public Calculator() {
              // TODO Auto-generated constructor stub
       }

       abstract void print();

       public int Addition(int parameter1, int parameter2) {
              return parameter1 + parameter2;

       }
}

Error:
The type SimpleCalculator must implement the inherited abstract method InterfaceForAbstractClass.display()

packagecom.tutorialbyexample;

public class SimpleCalculator extends Calculator {

       public SimpleCalculator() {
              // TODO Auto-generated constructor stub
       }

       @Override
       void print() {
              System.out.println("Print implementation from abstract class");
       }

       public static void main(String arg[]) {

              SimpleCalculator obj = new SimpleCalculator();
              obj.Addition(1, 2);
              obj.print();

       }

       @Override
       public void display() {
              // TODO Auto-generated method stub
             
       }
}



Abstract-Class-In-Java-Programming-Keyword-Example-Tutorial-Code-in-Eclipse


Errors & Solutions:

* Issue with final abstract, e.g.
public final abstract class Calculator {
}

* Class cannot be both final and abstract or The class Calculator can be either abstract or final, not both
Solution: We can't put final and abstract together. While putting final can't subclass.
public abstract class Calculator {
}

* Issue with static abstract, e.g.
public static abstract class Calculator {
}

Illegal modifier for the class Calculator; only public, abstract & final are permitted

Solution:

public abstract class Calculator {
}

* Issue we can't instantiate abstract class
* Cannot instantiate the type Calculator
Solution: Don't try to create object of abstract class.
Calculator obj = new Calculator();


Issue, if you not implement the abstract method in abstract class while implementing
The type SimpleCalculator must implement the inherited abstract method Calculator.print()

Solution: Implement abstract methods in implementation class what we have in abstract class.

Please like and share it!!! 
Ref:

Oracle Click me

No comments:

Post a Comment