Class Definition in JavaIn object-oriented programming, a class is a basic building block. It can be defined as template that describes the data and behaviour associated with the class instantiation. Instantiating is a class is to create an object (variable) of that class that can be used to access the member variables and methods of the class. A class can also be called a logical template to create the objects that share common properties and methods. For example, an Employee class may contain all the employee details in the form of variables and methods. If the class is instantiated i.e. if an object of the class is created (say e1), we can access all the methods or properties of the class. Defining a Class in JavaJava provides a reserved keyword class to define a class. The keyword must be followed by the class name. Inside the class, we declare methods and variables. In general, class declaration includes the following in the order as it appears:
Syntax:Java Class ExampleExample 1:Let's consider the following example to understand how to define a class in Java and implement it with the object of class. Calculate.java Output: Example 2:In the following example, we are creating two classes Employee and EmployeeClass. The Employee class fetches and displays the employee details. In the EmployeeClass, we create the objects of Employee class and use its methods. Here, we are initializing the objects using the class constructor. EmployeeClass.java Output:
Next TopicFind Saddle Point of a Matrix in Java
|