abstract class in java

abstract class in java

In Java, Abstract classes can have constructors even when they are only called from their concrete subclasses.

Java Abstract class can implement interfaces without even providing the implementation of interface methods. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation.

We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class.

refer to:‮ttual‬uri.com
// create an abstract class
abstract class Car {
  // fields and methods
  int speed;
  string color;
}
...

// try to create an object 
// It will throw an error
Car obj = new Car();

java abstract class example:

/**
 * @author lautturi.com
 * Java example: get the absolute value of a specified number
 */

import java.util.*;

abstract class Car {

	// method of abstract class
	public void display() {
		System.out.println("Car display");
	}
}

class Sedan extends Car {
}

public class Lautturi {

	public static void main(String[] args) {

		Sedan obj = new Sedan();

		// access method of abstract class using object of Sedan class
		obj.display();

	}

}
Created Time:2017-08-30 05:56:40  Author:lautturi