# 23) Abstract Keyword

## **What is abstract Keyword?**

**abstract** is a non-access modifier in java applicable for classes and methods but **not for variables**. It is used to achieve abstraction which is one of the pillars of Object Oriented Programming (OOP).&#x20;

Following are different contexts where abstract can be used in Java.

1. Abstract classes
2. Abstract methods

**Abstract classes**

We know that every java program must start with a concept of class that is without classes concept there is no java program perfect.

In java programming we have two types of classes they are

1\.     Concrete class

2\.     Abstract class

**Concrete class**

A concrete class is one which is containing fully defined methods or implemented method.

**Example**

```java
package com.gs.ilp.corejava.abstractKeyword;

public class Helloworld {
	void display() {
		System.out.println("Good Morning........");
	}
}
```

**Abstract class**

A class that is declared with **abstract** keyword is known as abstract class.&#x20;

OR, the class which is having partial implementation (i.e. not all methods present in the class have method definition).

In short, an abstract class is one which is containing some defined method and some undefined method.&#x20;

In java programming undefined methods are known as un-Implemented or abstract method.

**Syntax**

**abstract** **class** className

{

......

}

**Example**

**abstract** **class** Vehicle

{

......

}

**Abstract method**

An abstract method is one which contains only declaration or prototype but it never contains body or definition. In order to make any undefined method as abstract whose declaration is must be predefined by **abstract** keyword.

If any class has any abstract method then that class becomes an abstract class.

**Example**

**class** Vehicle&#x20;

{

**abstract** **void** Bike();&#x20;

}

Class Vehicle is become an abstract class because it have abstract Bike() method.

**Key Points:**

An instance of an abstract class cannot be created; we can have references of abstract class type though.

```java
package com.gs.ilp.corejava.abstractKeyword;

abstract class Base {
	abstract void fun();
}

class Derived extends Base {
	void fun() {
		System.out.println("Derived fun() called");
	}
}

public class Main {
	public static void main(String args[]) {
		// Uncommenting the following line will cause compiler error as the
		// line tries to create an instance of abstract class.
		// Base b = new Base();
		// We can have references of Base type.
		Base b = new Derived();
		b.fun();
	}
}
```

**Output:**

Derived fun() called

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.

```java
package com.gs.ilp.corejava.abstractKeyword;

//An abstract class with constructor
abstract class BaseCLass {
	BaseCLass() {
		System.out.println("Base Constructor Called");
	}

	abstract void fun();
}

class DerivedClass extends BaseCLass {
	DerivedClass() {
		System.out.println("Derived Constructor Called");
	}

	void fun() {
		System.out.println("Derived fun() called");
	}
}

public class MainClass {
	public static void main(String args[]) {
		DerivedClass d = new DerivedClass();
	}
}
```

**Output:**

Base Constructor Called

Derived Constructor Called

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.

// An abstract class without any abstract method

```java
package com.gs.ilp.corejava.abstractKeyword;

abstract class BaseClass1 {
	void fun() {
		System.out.println("Base fun() called");
	}
}

class DerivedClass1 extends BaseClass1 {
}

public class MainClass1 {
	public static void main(String args[]) {
		DerivedClass1 d = new DerivedClass1();
		d.fun();
	}
}
```

**Output:**

Base fun() called

Abstract classes can also have final methods (methods that cannot be overridden).

// An abstract class with a final method

```java
package com.gs.ilp.corejava.abstractKeyword;

abstract class BaseB {
	final void fun() {
		System.out.println("Derived fun() called");
	}
}

class DerivedD extends BaseB {
}

public class MainM {
	public static void main(String args[]) {
		BaseB b = new DerivedD();
		b.fun();
	}
}
```

**Output:**

Derived fun() called

**Why can’t we create the object of an abstract class?**

Because these classes are incomplete, they have abstract methods that have no body so if java allows you to create object of this class then if someone calls the abstract method using that object then what would happen? There would be no actual implementation of the method to invoke.\
&#x20;Also because an object is concrete. An abstract class is like a template, so you have to extend it and build on it before you can use it.

### Summary :

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTv2ytnWXfGlMHx5z47%2F-LTvCF3-5yrQNXyNIZ_L%2Fimage.png?alt=media\&token=f62d09a3-3375-45bc-92ea-c8bfcaad4a89)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTv2ytnWXfGlMHx5z47%2F-LTvCIk_nDW9WfHXwvNX%2Fimage.png?alt=media\&token=ab0a96ed-4a1c-4965-b5dd-cf288a6c6bb2)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTv2ytnWXfGlMHx5z47%2F-LTvCPPWVzq11aExrg6c%2Fimage.png?alt=media\&token=8a1e0374-763f-495c-9153-9e1127ab8aec)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTv2ytnWXfGlMHx5z47%2F-LTvCU3gs3ffgvdC8iMG%2Fimage.png?alt=media\&token=3457deae-8e47-4168-b9b5-4d01d588cfd6)

