Abstraction and Design Using Java 2e Programming Exercises Solutions

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.

Illustration: Abstract class

Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course .

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

Following are some important observations about abstract classes in Java.

  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. Abstract classes can not have final methods because when you make a method final you can not override it but the abstract methods are meant for overriding.
  5. We are not allowed to create object for any abstract class.
  6. We can define static methods in an abstract class

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 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 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();

}

}

Output

Base Constructor Called Derived Constructor 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 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();

}

}

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 you want to share more information about the topic discussed above.


Abstraction and Design Using Java 2e Programming Exercises Solutions

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

0 Response to "Abstraction and Design Using Java 2e Programming Exercises Solutions"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel