Abstraction in Java With Example Program With Output

In C++, if a class has at least one pure virtual function, then the class becomes abstract. Unlike C++, in Java, a separate keyword abstract is used to make a class abstract. An abstract is a java modifier applicable for classes and methods in java but not for Variables .

Illustration: Abstract class

abstract class Shape  {     int color;     // An abstract function     abstract void draw(); }

In java, following are some important observations about abstract classes are as follows:

  1. An instance of an abstract class can not be created.
  2. Constructors are allowed.
  3. We can have an abstract class without any abstract method.
  4. There can be a final method in abstract class but any abstract method in class(abstract class) can not be declared as final  or in simper terms final method can not be abstract itself as it will yield an error: "Illegal combination of modifiers: abstract and final"
  5. We are not allowed to create objects for an abstract class.
  6. We can define static methods in an abstract class
  7. We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract
  8. If a class contains at least one abstract method then compulsory should declare a class as abstract
  9. If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method

Let us elaborate on these observations and do justify them with help of clean java programs as follows.

Observation 1: In Java, just likely in C++ an instance of an abstract class cannot be created, we can have references to abstract class type though. It is as shown below via the clean java program.

Example

Java

abstract class Base {

abstract void fun();

}

class Derived extends Base {

void fun()

{

System.out.println( "Derived fun() called" );

}

}

class Main {

public static void main(String args[])

{

Base b = new Derived();

b.fun();

}

}

Output

Derived fun() called

Observation 2: Like C++, an abstract class can contain constructors in Java. And a constructor of an abstract class is called when an instance of an inherited class is created. It is as shown in the program below as follows:

Example

Java

abstract class Base {

Base()

{

System.out.println( "Base Constructor Called" );

}

abstract void fun();

}

class Derived extends Base {

Derived()

{

System.out.println( "Derived Constructor Called" );

}

void fun()

{

System.out.println( "Derived fun() called" );

}

}

class GFG {

public static void main(String args[])

{

Derived d = new Derived();

d.fun();

}

}

Output

Base Constructor Called Derived Constructor Called Derived fun() called          

Observation 3: In Java, we can have an abstract class without any abstract method . This allows us to create classes that cannot be instantiated but can only be inherited . It is as shown below as follows with help of a clean java program.

Example

Java

abstract class Base {

void fun()

{

System.out.println( "Function of Base class is called" );

}

}

class Derived extends Base {

}

class Main {

public static void main(String args[])

{

Derived d = new Derived();

d.fun();

}

}

Output

Function of Base class is called

Observation 4: Abstract classes can also have final methods (methods that cannot be overridden)

Example

Java

abstract class Base {

final void fun()

{

System.out.println( "Base fun() called" );

}

}

class Derived extends Base {

}

class GFG {

public static void main(String args[])

{

Base b = new Derived();

b.fun();

}

}

Observation 5: For any abstract java class we are not allowed to create an object i.e., for abstract class instantiation is not possible.

Example

Java

abstract class GFG {

public static void main(String args[])

{

GFG gfg = new GFG();

}

}

Output:

Observation 6: Similar to the interface we can define static methods in an abstract class that can be called independently without an object.

Example

Java

abstract class Helper {

static void demofun()

{

System.out.println( "Geeks for Geeks" );

}

}

public class GFG extends Helper {

public static void main(String[] args)

{

Helper.demofun();

}

}

Observation 7: We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract

Java

import java.io.*;

abstract class B {

abstract class C {

abstract void myAbstractMethod();

}

}

class D extends B {

class E extends C {

void myAbstractMethod() { System.out.println( "Inside abstract method implementation" ); }

}

}

public class Main {

public static void main(String args[])

{

D outer = new D();

D.E inner = outer. new E();

inner.myAbstractMethod();

}

}

Output:

Inside abstract method implementation

Observation 8: If a class contains at least one abstract method then compulsory we should declare the class as abstract otherwise we will get a compile-time error because If a class contains at least one abstract method then, implementation is not complete for that class, and hence it is not recommended to create an object so in order to restrict object creation for such partial classes we use abstract keyword.

Java

import java.io.*;

abstract class Demo {

abstract void m1();

}

class Child extends Demo {

public void m1()

{

System.out.print( "Hello" );

}

}

class GFG {

public static void main(String[] args)

{

Child c = new Child();

c.m1();

}

}

Output:

Hello

Observation 9: If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method.

Java

import java.io.*;

abstract class Demo {

abstract void m1();

abstract void m2();

abstract void m3();

}

abstract class FirstChild extends Demo {

public void m1() {

System.out.println( "Inside m1" );

}

}

class SecondChild extends FirstChild {

public void m2() {

System.out.println( "Inside m2" );

}

public void m3() {

System.out.println( "Inside m3" );

}

}

class GFG {

public static void main(String[] args)

{

SecondChild s = new SecondChild();

s.m1();

s.m2();

s.m3();

}

}

Output:

Inside m1 Inside m2 Inside m3

Must Read:

  • Difference between Abstract class and Interface in Java
  • Difference between Abstract class and Abstract Methods
  • Constructors in Java Abstract Class

 Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


kraftsualleadiang.blogspot.com

Source: https://www.geeksforgeeks.org/abstract-classes-in-java/

0 Response to "Abstraction in Java With Example Program With Output"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel