All About Class and Object in Java

0
118
Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares


Say we want to design a beautiful city. For that,we need to chalk out a plan for roads,houses,shops etc in a paper. Then we will design each of these individually,say a house. The blueprint of house will say how many doors will be there,how many windows,how many floors etc.Each of these houses will have its owner details when they are built and distributed.So in the sense the houses will look a like but the details of each houses like owner,how many occupants will be there in the house, will vary.Even the interior may vary from one house to another. But the base of creating a house will be same.This is same for roads,the design will be same but the name,locality and length will be different. It is similar to create a beautiful application using java. As a pure object oriented language,everything in Java should be inside a class.Java classes are the blueprint or master plan just like a master plan of a house.we can create any number of houses from this master plan. The master plan is called class and the individual houses created from this master plan is called object.

Again say our trainer has asked for a single pager resume. The trainer also has given a set of things in a document(Like -name,technology,experience,contact number ect).In real world this document is treated as template or blueprint and each individuals when created their own resume follow the same rules with different data. In this example,we can say the class is a template of the document and each document created from this template is called object.The objects will have their own set of data.

So in general the flow of the class and object creation is something like:
define a class–>create objects form the class—>send message to the objects

Aristotle was first to begin a study of the concept of type.He spoke about “the class of fishes and the class birds”.The concept that all objects while being unique ,are also part of the set of objects that have characteristics and behaviors in common was directly used in the first object oriented language(Simula-67).Its fundamental keyword class that introduces a new type into a program(class and type are often used synonymous).
A class is actually describing “a class of objects”.If we create a class of birds,any object belonging t this class will share these characteristics and behaviors.

 

A Class:

Classes provide a convenient method for packing together a group of logically related data items and functions that work with them.A class is given a name(called Identifier) to distinguish from other classes of the application.The class includes physical components called member elements and number of methods for operations. It is a datatype where the class creator will define how the new type of objects looks like.The linking and bonding between data and operations on these data is known as encapsulation.In the context of behavior, in approach, parameters to the behavior and return type should be primitive data type or a class object.

A class is an user defined data types with a template that provides the definition of the properties.Once the class type has been defined , we can create variables of that type using declaration that are similar to the basic type declarations that are similar to the basic type declaration. These variables are called instance of the class.(They are objects of that class)

The basic structure of the class is :


class className[extends superclass][implements someOtherclass]
{
[field declaration]
[methods declaration]
}
 
Filed declaration

Data encapsulated in a class by placing data fields inside the body of the class definition. These variables are called instance variables as they are created whenever an object of the class is instantiated. The declaration is exactly same as local variables.


class Rectangle{
int length;
int width;
}
 

Once they are declared , there is no space allocated to them.

Design of a class

While designing qualifier “-” signifies private and “+” signifies public.
like
-firstName=>String but private
+setFirstName(firstName)=> String but public

Collectively the variables and methods of a class are often referred to as members or fields of a class.
The transformation from a conceptual class diagram to class code is depicted here:

Class Diagram Class Code
MyClass ———————–> public class MyClass
-firstName:String ———————–> private firstName
+setFirstName(firstName:String) ———————–> public  void setFirstName(String firstName)
{
this.firstName=firstName;
}
+getFirstName():String ———————–> public String getFirstName()
{
return firstName;
}
so on….

the constructor is omitted here ,but in reality, we will have a constructor for a class.
Below is the general formula for a class:


<<Access specifier>> class className [extends SuperClass Name] implements interface1,interface2,....interfaceN{
//member elements mostly private
[Deceleration of member elements]
//methods mostly public
[Deceleration of methods]
}

 

Method declaration:
A class with only data fields has no life as there is no methods that operate on that data.The objects created by such class can not respond to any message.So it is necessary to design methods. Methods are declared inside the body of the class but immediately after the declaration of the instance variables.


[access modifier][specifier][return type]methodname(parameterList)
{
//method body
}
 

The four basic parts of the valid method are as follows:

  1. The name of the method(methodname)
  2. The return type of the method
  3. the list of parameters
  4. the body of the method.

We can pass object and some primitive data type to a method in the form of an argument and can return an object or primitive data type.
like setValue(int xyz);
anything inside the first bracket is considered as payload to the function.It is a mechanism by which we either pass value of data or reference to the object.Certainly a class have a function with empty payload .Like
setValue();


public class MyClass{
private String firstName();
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getFirstName(){
return firstName;
}
}
 

In the above statement, MyClass is an example of the structure of the class having one variable called firstName.If the constructor of the class MyClass() is called, the variable is set to the default value. In case, the constructor sets the value, it will be overridden.
Therefore every member variable will have a setter and a getter method so that the objects created from the class pass and change the values.
like setXXX(datatype value);//value passed
getXXX(){return datatype;}//method with no payload

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here