OOPs Concept - 1(Practicals)

What is class and object in OOPs?

Class

Let's take "Student" class for example,
Name of Class : Student
Attributes or Properties of Student class could be : name, rollNumber, course etc.
Operations of Student class could be : getDetails, setDetails etc.

Let's represent this Student class in Java.

class Student{

        private String name;
        private String rollNumber;
        private String course;
        //other attributes can go on here.

       public void setDetails(){
         //logic for setting student details will go here
       }

       public void getDetails(){
          //logic for setting student details will go here
       }
 }


For declaring a class we have to use class keyword followed by class name. Class name should be meaningful. It is considered as good practice if it relates to some real world entity. Then we define it's body or scope using {}. Within class scope or body we can declare it's attributes and operations.

Basic syntax for Attribute follows following convention :
<access-modifier> <data-type> <attribute-name>;

Access Modifiers could be private, protected and public. If none of these are specified then default or package modifiers will be assigned to attributes.
We will go through these modifiers in details in upcoming posts.

Data type defines what type of data, attribute is going to handle. In our case all three attributes are going to handle String type of data.
We will go through data types in details in upcoming posts.

Attribute name could be anything which relates to class.
Following conventions needs to be followed while naming an attribute :
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are ijkm, and n for integers; cd, and e for characters.


Object

If we want to use above "Student" class in our program than we have to create its object first. In Java objects are created using new keyword.

Basic syntax for creating objects in Java is :
<class-name> <object-name> = new <class-name>();

For example, if we have to create two objects of Student declared above then we can do as follow :
Student object1 = new Student();
Student object2 = new Student();

We can operate on this Objects using dot(.) operator of Java.
For Example :
object1.getDetails();
object2.getDetails();

This is the basic about classes and objects in java, we will continue to discuss this in subsequent posts.

Share:

Related Posts:

2 comments:

Popular Posts

Recent Posts

Followers

Total Pageviews

5,030