Java Class & Object Tutorial for beginners

A class is a template for creating objects which define their state and behavior. A class contains a field and method to define the state and behavior of its object. 

Syntax for Declaring Class:

<Access Modifier> class <Class_Name> extends
                 <Super_Class_Name> implements <Interface_Name>

ACCESS MODIFIER:  Defines who in the Java world can access this class and members of the class.

CLASS_NAME: Unique name for the class in a specific package.
SUPER_CLASS_NAME: Name of the class which given class extends. ( extends keyword is used for this purpose)
INTERFACE_NAME: Name of an Interface which the above class implements. (implements keyword is used for this purpose)

The internal structure of Class

<Access_Modifier> class <Class_Name> extends <Super_Class_Name> implements <Interface_Name>{

    <static initilizar block>

    <ananymous_block>

    <constructor_declarations>

    <field_declarations (Static or Non-Static)>

    <method_declarations (Static or Non-Static)>

    <Inner_class_declarations>

    <nested_interface_declarations>

    variables_inside_class(Static or Non Static)
}

Example of  Java Class

/*
 * This is Multi Line Comment and Comment can appear at any place
 */
package com.jbt;

import java.lang.*;

/*
 * As this file contains public class. Then the name of this file should be TestClass.java
 */
public class TestClass {
	public int i; // This is Non Static variable
	static {
		System.out.println("This is static block");
	}

	{
		System.out.println("This is ananuymous block");
	}

	TestClass() {
		System.out.println("This is constructor");
	}

	void methid() {
		System.out.println("This is method");
	}

}

class AnotherClass {
}

Classes are written in a Java source file. A source file can contain more than one Java Class. There are some rules associated with the Java source file as listed below.

Rules Applicable to Source file

  • There can be only one public class per source code file but it can have multiple non-public classes.
  • In case there is any public class present in the source code file, the name of the file should be the name of the class.
  • The sequence of statements in a source code file should be package >> import  >> Class declaration.
  • No Sequence rule is applied for Comments.
  • Comments can be there in any part of the source code file at any location.
  • Files with no public class can have any name for the class.
  • Import and package statements should be applied to all the classes in the same source code file.

Object

There is often confusion about the difference between Class and Object. A class is the creation of a prototype and an Object is the actual implementation of that prototype. Technically, Class is a template that describes what state and behavior an instance of this class can have. Object implements the state and behavior in the form of variables and methods and requires some memory allocated.

How to Create an Object of a Class

To create an Object of a Class <new> Keyword can be used.

Syntax:

<Class_Name>   ClassObjectReference = new <Class_Name>();

Here the constructor of the Class(Class_Name) will be executed and an Object will be created(ClassObjectReference will hold the reference of the created Object in memory).

Example

package com.jbt;

public class HelloWorld {
	int i; // Class Variable(State)

	void method() {
		System.out.println("Inside Method");
	}// Method (Behavior)
}

Object Create

HelloWorld obj = new HelloWorld();

How to Access the Member of a Class

(ClassObjectReference.member ). You will call the Method of an Object by naming the Object followed by a period (dot), followed by the name of the Method and its argument list.

objectName.methodName(arg1, arg2, arg3).

Bullet Points

  • A class can have only public and default access.
  • The public class needs to be in the same name java file.
  • A single java file can contain more than one non-public class but can have only one public class.
  • A public class can be seen by all classes from all packages
  • A class with default access can be seen only by classes within the same package.
  • A Java file with no public class has no naming restriction.
  • The Class can also have final, abstract, and strictfp non-access modifiers.
  • An Abstract Class cannot be instantiated.
  • A Final Class cannot be subclassed.
  • A Class cannot be both final and abstract.
  • Class visibility can be seen in 3 parameter
    1. If a class can extend another Class?
    2. If a class can create an instance of another Class?
    3. If a Class can access the methods and variables of another Class?

45 Comments

  1. Hi,

    A class with default access can be seen only by classes within the same package.
    Can you please explain what this line means..

    Class Java(){} Is this class considered as default or non public.

    • I really don’t know what you mean by Non Public. If you are not using any Access Modifier against class name it will be treated as Class with Default Access modifier. And given class is having default AM. And as it is having a default AM it means all the class in same package(It is different from same java file) can access given class. In order to understand please read about Package also.

      Regards

  2. ‘Class_name, Super_Class_Name, Interference_Name’, Can someone explain it in detail.
    What’s the diff between extends and implements.

    • Hi Sarang,
      As the name suggest, when you want to extend(or change) the behavior of class you will use “extend” keyword with given class. And implement is used when you are going to implement the behavior, which is the case in Interface.

      Regards

  3. In Bullet Points….. “An abstract class cannot be initiated” it should be “An abstract cannot be instatiated”…. please verify it

  4. In the introductory para you have said: “In case there is any public class present in the source code file, name of the file should be the name of the class”. Does this mean ( pl see ” In case”) there can be a source code without public class? If so, the file name can be that of any class in the package or any other name also ?

    • Yes. A file can exist without any public class. But if there is a public class file name should match with same.
      In this way you can have more than one public class in a file.

      Thanks

    • Their is no link between these two. Variables & method are for two different purpose. Variables are to maintain state while methods are used to do some calculation or perform some business logic.

      Thanks

  5. The objects and class with example shud be mention, beginners don’t know about this and also explain what is n mean by instance of class

  6. It will be great if you will underline the difference between the class and object. It’s very important for the beginner java developers.

    • I am giving you a very basic and easy definition between class and object in java:

      1. “Class” in java can be assumed as any Infrastructure or building, and we create room
      withing that building to store our valuable, so this room can be assumed as “object.”
      2. Just like without building we cannot create rooms, like wise only without class we can
      not create object.
      3. Rooms are always inside the building, like wise objects are always inside the class.

      **** Now, coming towards accessing the objects:
      # There are 4 Access Specifier of any class:
      1. public 2. private 3. protected 4. default

      1. Any one can access to any rooms of the public building, like that only we can access
      objects of public class.
      2. Only the Residents can access to the room of the private building like wise only objects of
      private class are accessed inside the class only.
      3. If two friends are staying in one room of the same building and then after sometime one
      got shifted to another room of the same building, then there is a case that the guy who
      shifted to other room can have access to the another guy room, like wise only the object
      of the protected class can be accessed withing the same class as well as by all objects of
      that class.
      4. default class are known as “Package-private class” it is visible withing the same package
      only.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.