Java: Class


A Class is a blueprint or prototype from which objects are created. A class has properties and behaviors i.e methods that are common to all objects of the same type.

Object Class

Syntax of Class Declaration


Access_Modfier Non_access_modiers class class_name 
    extends super_class 
    implements interface1, interface 2..
{
fields
......
default constructor
......
parametrize constructor
......
methods
......
}
  • Access Modifiers: A modifier defined access scope of class, fields, and methods. If specifically not mentioned consider as default. See Also: Java: Access Modifiers/Specifiers
  • Class name: The class name should begin with an initial capital letter follow camel notation. See Also: Java: Identifier Naming Conventions
  • Non-Access Modifiers (if any): non-access modifiers can also be used on the class level to make a class special. See Also: Java: Non-Access Modifiers
  • Superclass(if any): A class can extend only one class i.e called parent class or superclass.
  • Interfaces(if any): A class can implement one or more interfaces. Their interfaces proceed by keyword implements and separated by a comma.
  • Body: A class body is surrounded by curly braces, { }.
    Fields: A class fields are variables that provide the state of class and it’s objects.
  • Methods: class methods are defined to implement the behavior of class and objects. See Also: Java Methods
  • Constructors: Java class constructors are used to initialize new objects. See Also: Java: Constructors

Note:

  • If a class is declared with access modifier public then java file name would also be the same. One Java file can have only one public class.
  • All the class extends Object Class i.e Object class is the superclass of all the classes.See Also: Java: java.lang.Object Class & Methods

Java Class Example

This is a very simple example of a class name as Animal. It’s having variables, constructors, methods and overriding method of superclass Object.

//Class Declaration
public class Animal {
	//instance variables
    String name;
    String breed;
    int age;
    String color;

    //Default Constructor : Constructor without parameter
    public Animal()
    {
    this.name="Default";
    }

   // Parameterize Constructor : Constructor with parameter
    public Animal(String name, String breed,
                   int age, String color)
    {
        this.name = name;
        this.breed = breed;
        this.age = age;
        this.color = color;
    } 

    //Instance methods
    public String getName()
    {
        return name;
    } 

    public String getBreed()
    {
        return breed;
    } 

    public int getAge()
    {
        return age;
    } 

    public String getColor()
    {
        return color;
    }
    //methods override from Object class
	@Override
	public String toString() {
		return "Animal [name=" + name + ", breed=" + breed + ", age=" + age + ", color=" + color + "]";
	}
}

Now we have got a basic understanding of class declaration and implementation. In the further post, you will learn about the types of classes and uses.

Types of classes

Java supports lots of types of classes:

  • Concrete Class
  • Abstract Class
  • POJO Class
  • Static Class
  • Nested Class/Inner Class
  • Final Class
  • Anonymous Class
  • Lambda Expression

Follow this link to know about all these classes and uses.

See Also: Java: Type of classes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s