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
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
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 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 introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

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

Introduction to object oriented programming

Till now, we have written a program under the guise of a class though what we have actually written was just a program and not a class in the real sense. Now, we shall start programming the object oriented way. In the due course of being taught the way to write simple programs, you have also been told a great deal about classes and objects. We shall now recollect all that we have learnt about object oriented programming, classes and objects. We shall also look into the three features of object oriented programming.

Classes and objects are the basics of objected oriented programming. In an objected oriented model, the focus is on data (contained in objects) and not on the code that manipulates the data. A class contains instance variables, constructors and methods. We instantiate (create) objects from these classes by invoking (calling) the constructors. Each object created from a class has a copy of the instance variables. Each object manipulates its instance variables independently using the methods of the class. While variables represent an object, the methods help in modification of these variables and accomplishment of tasks.

Object oriented model of programming helps to simulate real world objects in a better way. Let's discuss how this is done by considering a 'Car 'class as an example. The class can have instance variables such as current Speed, quantityOfPetrol, and so on. We can provide methods such as startCar(), stopCar(), chageSpeed() and so on. Some of these methods do not require any additional information while a few others do require. For instance, startCar() and stopCar() doesn't require any additional information while changeSpeed() requires the speed to which the car is to be raced. This information that is to be passed is known as an argument. In conclusion, we have designed the 'Car' class and we are now capable of building a number of Car objects from them such as 'Car1', 'Car2' and so on. Each of these objects function independently from each other by maintaining their own sets of variables. We can, of course, make different objects such as 'Car1' and 'Car2' interact with each other. We have now a world of cars. We have been able to successfully build a virtual world. That is what object oriented programming is all about- simulating the real world using objects built from classes.

There are three principles of object oriented programming- abstraction, inheritance and polymorphism. We shall look at them one by one.

Abstraction is the process of representing the essential features of a system without getting involved with the its complexities. Consider the Car example again. Once you write the 'Car' class, other people will be able to create Car objects and use them. You need not disclose to them how you implemented the starting and stopping of the Car. All that you need to tell them is how those methods can be called. This is what abstraction is all about. Other people are capable of using your Car class, create Car objects, and call methods such as startCar() and stopCar(). They are capable of starting and stopping the car without knowing how it is being done. Complexities have been hidden. Only the essentialities have been exposed.

The next feature is inheritance. It is the capability of a new class to inherit the variables and methods of other classes. The new class can define its own variable and methods in addition to what have been inherited to provide additional capabilities to the new class. It is similar to how sons and daughters inherit property and can add their own earned property to the inherited property. If we talk in terms of the Car example, a new class called SuperCar can be made to inherit from the class Car so that it will have the functionality of starting, stopping and changinging speed and in addition will be able to perform some special tasks like flying in the air similar to what we see in Hollywood movies.

The final feature of object oriented programming that is to be discussed is polymorphism. This is the property by which different objects can react to the same method call or in simpler terms a message, and still produce the correct output i.e. the desired output. If we talk in terms of our Car, then we can state the feature of polymorphism as the ability to change speed to both a numeric value and a fractional value. Different methods in those classes will handle these calls but still the message that would be passed would be the same, which is changeSpeed(). The only difference is in the arguments (the information to be passed). While one call passes 34 as an argument, the other call would pass 34.7 as the argument. These things might appear trivial for now but you will be able to appreciate these features later on when we deal with how these features are implemented in Java.

Hoping that you understood what object oriented programming is, we now move on to the details of how classes are defined in Java. For the rest of our discussion about building classes, we will focus on building a Student class. A student will be having a name, a roll number and marks in three subjects. Once the class is built, we should be able to create Student objects. And there should be methods for finding the total marks, the average of two students and so on. In the due course of time, we will also see how we can assign the roll numbers automatically to Student objects starting from 1. We shall also explore some of the other countless possibilities.

Privacy Policy