Java4teachus

Thursday, August 23, 2018

Syntax Of a Class in Java

Definition Of Class


A class is collection of Data members where members can be defined as member variable and member methods within a Block of statements is called class. In java a class can be written by using the keyword called "Class". A class is related to properties and Fields is also known as Abstract data type.

Syntax Of a Class

   class  <ClassName>
   {
        Variable declaration (Or) Data members ;
        Methods definition ;
   }

➠ The members of a class can be public or public. In java, we cannot have global variables or stand alone functions. If a variable or a function has to be written, It must be a member of some or the other class. This is also reason java is a "Object oriented programming language".

➠ Method definition has four parts. They are:
  • The name of the Method( Method Name)
  • The type of the value the method return (type)
  • A list of parameters .
  •  The body of the method.                                                                 
In the below diagram shown as class name called Tree. But a Tree is one of the class name and different type of trees is called  "Object".

Different-type-of-classes-with-class-name-called-Mobile.png


In the above diagram shows class name called Mobile. But here Mobile is one of the class name and different type of Mobiles is called  "Object".

Different-type-of-classes-with-class-name-called-Tree.png

Explanation

  • Class is one of the keyword which is used for developing "Programmer Defined Data type" and to model the new world applications.
  • <class name> is one of the java valued  variable name treated as name of the class. In java if any  class contains either one word more than one word then words all first must be capital. This rule is also known as Hungarian rule in java.
               Example:-  class StudentInfo, class EmployeeInfo .
  • In java each and every name  is treated as  Programmer Defined Data Type.
  • Variable declaration represents set of data members which is selected from based on <class name>.  
  • Methods definition represents the set of methods meant for performing different type of operation. In java all the Methods must be defined inside the class only but not outside of a class.
  • In oops concepts we have 2 types of methods they are:    
           a) Member method             
           b) Non-Member method
  • A Member method is always available with in the scope of the class and it can access the values present in the data member.
  • A Non- member is not available within the scope of the class and they cannot access values present in the data member.
  • Java programmer permits only member method for providing high security and never permits non-member methods.
  • The definition of the class must be embedded within open bracket ( { ) and closing bracket ( } ). The definition of the class may are may not be terminated in semicolon ( ; ).
  • Hence all the class definition contains "logical existence" and acts of formula.
  • Define a class for representing student information.    
class Student
{
int std no :
String std name;
float M 1 ,M 2 ,M 3;
int getNoOfHoursStudy ( )
{
Return (3)
}
float getTotalMarks ( )
{
float total =m 1 +m 2 +m 3 ;
return total ;
}
String getGrade ( )
{
Return” distinction” ;
}
}

No comments:

Post a Comment

Thanks for visiting my site..