Category Archives: Java

Abstract Class in Java

Java Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

Like any other class, an abstract class can contain fields that describe the characteristics and methods that describe the actions that a class can perform. An abstract class can include methods that contain no implementation. These are called abstract methods. The abstract method declaration must then end with a semicolon rather than a block. If a class has any abstract methods, whether declared or inherited, the entire class must be declared abstract. Abstract methods are used to provide a template for the classes that inherit the abstract methods.

Abstract classes cannot be instantiated; they must be subclassed, and actual implementations must be provided for the abstract methods. Any implementation specified can, of course, be overridden by additional subclasses. An object must have an implementation for all of its methods. You need to create a subclass that provides an implementation for the abstract method.

Java – How to read input from console

JDK 5.0 provides a feature to read input from console. Java.util,Scanner is used for this purpose. This code read a String and an Integer from the console and stores them in the variables.
import java.util.Scanner;

public class InputExp {

public static void main(String[] args) {

String name;
int age;
Scanner in = new Scanner(;

// Reads a single line from the consoleĀ  and stores into name variable
name = in.nextLine();

// Reads a integer from the consoleĀ  and stores into age variable

// Prints name and age to the console
System.out.println(“Name :”+name);
System.out.println(“Age :”+age);