Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Abstract Class

Java Abstract Class

shape Introduction

A concrete class in Java is the default class and is a derived class that provides the basic implementations for all of the methods that are not already implemented in the base class. The opposite of the concrete class is the Java Abstract Class, which does not provide implementations for all of its methods. Java Abstract Class, Before knowing about the Java Abstract Class, user need to have knowledge of abstraction, abstraction is nothing but hiding the data, for example, while sending any message to others no one know what is happening inside but it is possible by two ways as follows.

shape Description

All abstract methods must be declared using “abstract” keyword. If a class containing at least one abstract method, then the class must be declared as abstract. If a class declared as abstract as it is not mandatory to develop abstract methods.

shape Conceptual figure

There should be a IS-A relation ship between below two classes.

shape Example

[java] public class Abstract3 { public static void main(String[] args) { System.out.println("Program starts"); A dRef1=new A(); dRef1.test1(); System.out.println("Program ends"); } } abstract class Sample1 { abstract int test1(); } class A extends Sample1 { int test1() { System.out.println("test1() is defined in class A"); return 12; } } [/java] Output: [c] Program starts test1() is defined in class A Program ends[/c]

shape More Info

shape Example

[java] package com.spl.abstract1; public class Abstract1 { public static void main(String[] args) { System.out.println("Program starts"); B dRef1=new B(); dRef1.test1(); dRef1.test2(); System.out.println("Program ends"); } } abstract class Sample1 { abstract int test1(); abstract void test2(); } abstract class A extends Sample1 { int test1() { System.out.println("test1() is defined in class A"); return 12; } } class B extends A { void test2() { System.out.println("test2() is defined in class B"); } } [/java] Output: [c] Program starts test1() is defined in class A test2() is defined in class B Program ends[/c]
  • Above example class A preaches or violets the contract of sample 1 abstract class because class A is not defining test 2() method which is still abstract in the class A.
  • Class B is a concrete class even though it is not overriding all the abstract methods because class B contains only test 2()method as an abstract that method is defined in class B).
  • Where as test 1() is already made concrete in the super class A.

shape Example

[java] package com.spl.abstract2; public class Abstract2 { public static void main(String[] args) { System.out.println("Program starts"); A dRef1=new A(); dRef1.test1(); System.out.println("Program ends"); } } abstract class Sample1 { int test1() { System.out.println("test1() is defined in class Sample1"); return 12; } } class A extends Sample1 { } [/java] Output: When compile the code following is the result will be generated. [c] Program starts test1() is defined in class Sample1 Program ends[/c]
  • In the above example the class B is becoming concrete before fulfilling the contract of abstract class because the abstract class doesn’t have any abstract method that has to be overridden by subclass. Hence, abstract class is not 100% abstract.
  •   An abstract class can have abstract method, data member, constructor, method body and even main() method also. The following is an example. [java]package com.splessons; abstract class Java{ Java(){System.out.println("I am java");} abstract void run(); void tutorial(){System.out.println("I have all tutorials");} } class database extends Java{ void run(){System.out.println("running safely..");} } class Splesson{ public static void main(String args[]){ Java obj = new database(); obj.run(); obj.tutorial(); } } [/java]

    Summary

    shape Key Points

    • To achieve 100% abstract, develop all the methods in abstract class as abstract or we can use interfaces.