Java Class & Object Tutorial for beginners

This entry is part 3 of 33 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 java world can access the class and the members of the class.

Class_Name
Unique name for the class in a specific package.
Super_Class_Name
Name of the class which the above 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)

 


 

Internal structure of Class


 

Example of  Java Class

Classes are written in a java source file. A source file can contain more then one java class. Below are the rules related to java source code file.


 

Rules applied to Source code 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, 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, there is no rule applied for the same.
  • Import and package statements should be applied to all the classes in the same source code file.

 

How to Create an Object of Class

To create object of  a class <new> Keyword can be used.

Syntax:

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

Here constructor of the class(Class_Name) will get executed and object will be created(ClassObjectRefrence will hold the reference of created object in memory).


 

How to Access Member of a Class

(ClassObjectReference.member ). You call a method of an object by naming the object followed by a period (dot), this should be followed by the name of the method and its argument list.

 

Bullet Points

  • Class can have only public and default access.
  • Public class needs to be in same name java file.
  • 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 package
  • A class with default access can be seen only by classes within the same package.
  • Java file with no public class have no naming restriction.
  • Class can also have final and abstract & strictfp non access modifiers.
  • An abstract class can not be initiated.
  • 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 >>

About the author

J Singh

Main brain behind this blog/QnA site. She has written all these article as per her personal experience. She has published it mainly to help beginners. These articles might have some problems. So bear with her and let her know if you find any problem in any of the article.

11 Comments

Leave a Comment