Java Class & Object Tutorial for beginners

This entry is part 3 of 34 in the series Core Java Course

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

Syntax for Declaring Class:

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 above class implements.( implements keyword is used for this purpose)

Internal structure of Class

Example of  Java Class

Classes are written in a Java source file. A source file can contain more than one Java Class. There are some rules associated to 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.
  • 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 on the difference between Class and Objects. Class is the creation of a prototype and Object is the actual implementation of that prototype. Technically, Class is a template which describes what  state and behavior of an instance 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

Object Create

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.

Bullet Points

  • Class can have only public and default access.
  • The public class needs to be in same name java file.
  • A single java file can contain more then 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 have no naming restriction.
  • The Class can also have final, abstract and strictfp non access modifiers.
  • An Abstract Class can not be instantiated.
  • A Final Class can not be subclassed.
  • A Class can not 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 instance of another Class?
    3. If a Class can access methods and variable of another Class?
Series Navigation<< jdk vs jre vs jvmConstructors in Java >>

32 Comments Java Class & Object Tutorial for beginners

  1. Java Dev

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

    Reply
    1. Pushp Singh

      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.

      Reply
        1. ganesh

          if u post a message or photo or video on u r time line
          in that case u will select “Privacy mode”
          i.e 1.if u select public ,u r post will all peoples are seen
          2. if u select private u only seen
          3.if u select protected u r friends(u r frnd list ) only seen

          Reply
  2. Pingback: Java Beginners TutorialGetting started with Java

  3. shruti

    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

    Reply
    1. Vivekanand Gautam

      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

      Reply
  4. Balan

    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 ?

    Reply
    1. Vivekanand Gautam

      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

      Reply
  5. Madhavi

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

    Reply

Leave A Comment

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