Menu
Topics Index
...
`

Java Class & Object

In object-oriented programming, a class can be treated as a template definition of the methods & variables in a specific type of object. So, an object is a particular instance of a class; instead of variables, it has real values. Simply, a Java class is a template which is for creating objects.

In object oriented programming, a class is a defining idea. The key points about a Java class are:
  • A Java class contains subclasses which can inherit all or few of the characteristics of the class. By relating to every subclass, the class can become a superclass.
  • Even subclasses are also able to defining their own methods & variables which are not belongs to their superclass.
  • The levels of a class and its subclasses are called the class hierarchy.

A class declaration is done by defining the keyword 'class' followed by a identifier which is non-reserved that names the class. A pair of open braces and close braces will start the class body and ends it. The syntax of a Java class can be as follows:

  • class identifier
    {
          //class body
    }

The first letter of a Java class must be uppercase and the remaining characters are in lower case. For example, the class name Student. If a class name consists of more than one word then combine the words without spaces and fist letter of each word must be in uppercase. For example, the class name StudentDetails. This type of name convention is called as Camelcasting.

The following example shows you, how to define a class name:

  • class StudentDetails
    {
          //class body
    }

A class body is contained by various fields, constructors and methods. Grouping all these language features into classes is defined as encapsulation. This type of capability lets us the program at a high-level abstraction (classes & objects) instead of focusing on data structures & functionality separately.

A Java object is a class instance. In other words, an entity which has state & behavior is called as a Java object. For example, a dog has states like name, color, breed and also behaviors like barking, wagging, eating, etc. Every Java object contains some states and behavior. In real world, we can have so many objects around us like humans, pens, cars, bikes, etc .

If we comparing the software objects with real world objects, both have same type of characteristics. Software objects can also contains states and behaviors. A software object states are stored in fields & behaviors can be shown through methods. In software development, methods are operating internally for an object and communication between the objects can be done via methods.

Java object is a physical thing as-well-as logical entity (tengible and intengible ), but Java class is a logical-entity only. Banking system is a example for intengible Java object. A Java object mainly contains three characteristics.

  • state: Which represents value (data) of an object.
  • behavior: Which represents an object's behavior (functionality) like transfer, withdraw, deposit, etc.
  • identity: Object's identity can be implemented by a unique id. The id value can't visible to the external users. But, it can internally used by the JVM for identification of each Java object uniquely.
A Java object can be created in different ways. They are:
  • using 'new' keyword.
  • using newInstance() method.
  • using factory method.
  • using clone() method. etc.
The topics, we can cover in this section are Java classes and objects, Java Object References, Member Variable In Java, Class References And Objects In Java, this keyword in Java, Example program using classes, etc which explained in detail below.


In this section, we'll discuss more about:
8.1 Introduction to Java Class
  • Classes are supported by every object oriented programming language and they can be used to group related data. For e.g., if we take student details, although we can store...Read More

8.2 Java Class and Java Object
  • Here are few points which we need to understand about Java classes and objects. (a) A Java class is like a blue print and we can create as many objects using that class. (b) All the objects...Read More

8.3 Java Object References
  • Creating an instance (or object) of Student class (or type) has to be done using the new keyword. This is different when compared to the creation of primitive types. We can create...Read More

8.4 Member Variable In Java
  • A class comprises of various variables (or attributes). For e.g., the Student class is comprised of variables name, marks and section, the Car class is comprised of variables ...Read More

8.5 Class References And Objects In Java
  • A Java class is also referred as type and an object is also referred as instance. Here is the example Java program which is about class references and objects. It will give the clear...Read More

8.6 To Print Student Details Using Classes In Java
  • To Print Student Details Using Arrays has some problems like not able to add or delete the student since the info related to one student is present in multiple arrays. We can use classes to...Read More

8.7 Create Objects Using Constructor In Java
  • Constructors is a special mechanism using which, the member variables of an object can be initialized when the object is being created. Constructors also ensure that no ...Read More

8.8 Class With Multiple Constructors In Java
  • The constructor of a class is used to initialize the member variables and perform any other setup. Some times it is required to have multiple constructors to initialize the ...Read More

8.9 this Keyword In Java
  • this is one of the java keywords and it helps in referring to the current object. When we use the dot(.) operator on this keyword, then we can access the member variables of ...Read More

8.10 Behavior Of Java Classes Using Methods
  • Classes can be used to group related data. Methods using this data can be defined in the class. The methods created in the class are similar to the methods in Java. Every ...Read More

8.11 Java Multiple Methods In One Class
  • We know that we can create methods in a class. But we can also able to create multiple methods in one class. The following program shows how we can create multiple ...Read More

8.12 Calling A Class From Another Class In Java
  • We can have classes as part of another class. i.e. We can have a user defined type inside another user defined type. e.g., room having fan and lights, car having engine and ...Read More

8.13 Creating A Class For Data Validation
  • One more advantage of using classes is that we can ensure that the data is valid. e.g., the marks of a student should be always between 0 and 100 and allowed sections are ...Read More

8.14 Java Program To Find Rectangle Area & Perimeter Using Classes
  • Here is the example program in Java to find Rectangle Area and Perimeter using classes. Here we have created a Rectangle class with member variables length and breadth. We also...Read More

8.15 Java Program to Find Area of Various Shapes Using Classes
  • Here is the program which shows to find areas of different shapes like rectangle, square & circle and find the shape with the largest area. Here we have created three ...Read More

8.16 Java Program To Compare Movies
  • This example shows how to compare the various entertainments like movie, drama and circus. We compare the collections made by each entertainment. We also print the ...Read More

Dependent Topics : 7. Arrays 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App