Reading:  

Quick introduction to Java Programming language - Part 1


Objects and Classes

Object Oriented Programming System (OOPS):

Object-Oriented Programming is a technique used to design a program with the help of classes and objects.

  • Object
  • Class
  • Polymorphism
  • Inheritance
  • Abstraction
  • Encapsulation

Object:

It is an entity which has state and behavior. Examples for object are: Fan, table,pen, car etc.

Definition: Object is an instance of a class which has the state which exhibits the well-defined behavior and having unique id.

The characteristics of object:

  • State: represents value of an object.(Eg: Car color, Power,name)
  • Behavior: represents the behavior of an object such as Car Running State and OFF State etc.
  • Identity: Object has unique ID. The JVM identify each object uniquely where the value of the ID is not visible to the external user.

Example: Car is an object, name is Swift and color is Redetcknown as its state. It is used to move from one place to another, so moving is its behavior.

Class:

Class is a collection of object which share common states and the behavior.

A class has the following:

  • data member
  • method
  • constructor

Syntax of a class:

public class JavaProgram
{
  int i=10;   // Data Member
  public void getNumberValue()      //Method
   {
    //Method to get the number value 
   }
}

Constructors:

Constructor is a special type of method which will be implicitly invoked and executed when object is created using “new” keyword. Constructor is mainly used to initialize the data-members.

Syntax: Example

public class JavaProgram
{
  int i=10;   // Data Member
// I am a default constructor
public JavaProgram(){
System.out.println("I will be printed first");
} public void getNumberValue() //Method { //Method to get the number value } }

Characteristics of Constructors:

  • Constructor name should be same name as class name
  • Constructor does not have any return type, not even void
  • Constructor is called at the time of object creation only

Object Creation:

A class is blueprint for objects. Using new keyword will create objects as shown in below example.

public class StringDemo
{
  public void subjectName()
  {
     String name=”Java”;
  }
  public static void main(String[] args)
  {
   StringDemo d1= new StringDemo();  //object creation using new keyword
   /* Note: Here d1 is object reference variable. */
   d1.subjectName();   //calling/executing above method using created object
  }
}

 

To access instance Variables and Methods using object reference:

Below examples shows how to access the instance variables and methods by using created object.

public class Account {
	String name;
	intact_id;
	Double balance;
	
	void withDraw(double amt)
    {
	   System.out.println("Withdraw amount: "+amt);
    }
    void deposit(double amt)
    {
	   System.out.println("Deposit amount: "+amt);
    }
    public static void main(String[] args) 
	{
		Account a1 = new Account();
		a1.name = "Java";
		a1.act_id = 120;
		a1.balance = 3000.0;
		a1.withDraw(200);
		a1.deposit(100);
	}
} 

 

The output is shown below:

Java Package:

Packages are the folders which are used to group the classes, interface and files.

Points to remember:

  • To declare the package we use package keyword followed by package.
  • Package name is reverse of the domain,package name should be in lower case.

Syntax:

1.import <packagename>.*; 
2.import <packagename>.className;

Example: import java.io.*;

  • A class can access other classes which are in same package.
  • Declaring the package should be first statement in the program.

Description

This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include 

  1. Java basics
  2. Object oriented programming
  3. Advance concepts

Each part is subdivided in multiple chapters.

Java basics has the following chapters

  1. Introduction
  2. Environment Setup
  3. Basic Syntax
  4. Objects and Classes
  5. Basic Data Types
  6. Variable Types
  7. Modifier Types
  8. Basic Operators
  9. Loops
  10. Decision Making
  11. Numbers Class
  12. Character and String Class
  13. Arrays
  14. Date and Time
  15. Regular Expression
  16. Methods
  17. Streams, Files and I/O
  18. Exceptions Handling

Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.



Audience

Beginners or students seeking a refresher on Java language

Author: Subject Coach
Added on: 9th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!