Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


Abstract Classes and Methods

A method is declared with the abstract specifier if we wish to only declare it and not provide its implementation. For instance, in the previous example, we could have defined the method move() as abstract if we are sure that three does not exists any general Animal.

public abstract void move();

Note that there is a semicolon at the end of the method declaration which indicates that the method is not yet defined. A class containing one or more abstract methods should also be declared as abstract. Mentioning something as abstract simply means that the item under context is not yet usable and further actions are necessary to make it usable.

abstract class Animal {
    //code
}

We may also declare a class as abstract even when the class contains no abstract methods. But when a class contains atleast a single abstract method, the class also needs to be abstract. Otherwise, compilation errors occur. Objects of an abstract class cannot be created. Even then, constructors may be provided for an abstract class for use by its sub classes. An abstract class is made usable by extending it to a sub class. The sub class needs to provide implementations of all the abstract methods defined in its super class, just in the way methods are overridden. No special syntaxes exist for it. If one or more of the abstract methods of the superclass are not defined in the sub class, even the sub class needs to be defined as abstract. In addition, it may declare its own abstract methods. When a new class extends another class, it needs to provide implementations of not just the abstract methods of its immediate superclass but also of all the other indirect superclass's lying in the hierarchy. Though we may not create an object of an abstract class, we are free to declare a variable of that abstract class type. We can then assign references to objects of the subclass type to these superclass variables.

Animal a = new Bird();

The abstract word is often used in association with the advantage provided by polymorphism. For instance, in the example we dealt with, creating an Animal object might seem absurd. Hence, we may declare it to be abstract with no other changes required. During compilation, the method call move() would be resolved to the version in Animal class itself even though it has been declared to be abstract. This is because, any object reference that has been assigned to this variable is guaranteed to have an implantation of the move() method failing which, an object of the Animal type (by way of inheritance) could not have been created in the first place.

The keywords abstract and final may not be used together. This is because an abstract method or a class should be extendable by other classes. Otherwise, there is no point in defining those classes.

abstract final class A // compilation error
abstract final void meth(); // compilation error

Privacy Policy