Classes and Objects in JAVA JAVA by Ravinder Nath Rajotiya - April 18, 2021April 18, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle Classes and Objects in JAVAConstructors:Default Constructor:parameterized contructorsMethod or constructor overloading:Setter and Getter methods:Getter methods:Generating getter method automatically:Setter method in JAVA:Setter method automatically:Objects:Here is the complete example:class Housemain function Classes and Objects in JAVA Everything that we see around us are objects for example people, metals, car, tree, bike, animals and so on. Every object belong to certain class. So, we have to understand their classification to differentiate among them. Class is way of describing some properities, functionality or behaviour of an objects. It is a template or blueprint for an object. For example a house class will have four properties- windows, doors, roof, walls. so we can create the following properties of one house and their number or type may differ from hour=se to house. House Object-1 House Object-2 House Object-3 Property Numbers or type Property Numbers or type Property Numbers or type Windows 20 Windows 10 Windows 8 Doors 4 Doors 3 Doors 2 Type of roof Tiles Type of roof Grass Type of roof Wood Walls Bricks with plaster Walls Bricks Walls plaster As seen in above table all house have same properties but their values differ. In JAVA we can create class house for example to have four prpoerties as nrOfWindows nrOfDoors typeOfRoof typeOfWall Let us write a program involving house as a class with abve properties in Intellij house properties with access specifiers as private will have access within the same class. No where else but in this class we can use these variables. public class specifier means that this class can be accessed anywhere in this package or outside this “com.example” package Constructors: constructors is a method in a class that is always be public and with the same name as class. It will never return any value not even void. Constructor is used to construct the object of the class. public House(){ nrOfWindows =10; nrOfDoors = 2; } we may not initialize all properties inside the constructor. We can later assign them values using any set method. Default Constructor: If you do not define a constructor inside a class, JAVA will automatically construct a one for you with all number type properties initalizes to default ‘0’ and object type properties set to null, boolaen varibles if any will be set to false. A default constructor is constructed when you do not supply the constructor from the object. A default cnstructor will look like: public House(){ nrOfWindows =0; nrOfDoors = 0; typeOfRoof =null; typeOfWall = null; } parameterized contructors In JAVA we can provide constructors that accepts class parameters as aruments. But, it may be confusing as to which object values the constructor parmeters refers to, you can prefix this. with parameter variables. Then this.nrOfDoors on the left of ‘=’ will refer to the object calling the constructor and on the right of ‘=’ as the argument of consructor passed from object. here is an example public House(int nrOfWindows, int nrOfDoors, String typeOfRoof, String typeOfWall) { this.nrOfWindows = nrOfWindows; this.nrOfDoors = nrOfDoors; this.typeOfRoof =typeOfRoof; this.typeOfWall = typeOfWall; } Method or constructor overloading: As seen above we have two constructors on without any argument and second with class parameters as arguments. As both constructots have same name but different argument, this concept is called constructor or method overloading. Setter and Getter methods: Now as we have defined all our properties as private which are not accessible any where but in the class. We would need some public methods which will be used to get or set the values of these paramenters Getter methods: A getter method is one that returns values for those specific class properties. Because nrOfWindows is of type int, so getter method will be of return type int. getter method starts with get followed by upper cse letter of property name. The object when calls getNrOfWindows() will return nrOfWindows. Here it is: public int getNrOfWindows() { return nrOfWindows; } Generating getter method automatically: Step-1: Right click in work space window Step-2: Choose Generate Step-3: Select Getter Step-4: Select the paropertis which are required in getter methods Step-5: Click OK And you can now see that the IntelliJ auomatically creates the rest of getter methods for other properties like the above getter methd. We can chhose the getter method names as anything of our choice, but it is JAVA convention to use them as name of properties. Setter method in JAVA: What if you want to change or set the values for the propries of an object. there come the setter method. Its return type as void as it will not return anything. its name is set followed by the name with first character as capital. It is JAVA convention to have seet method name same as object property name. It get the argument which is required to be set. Here it goes. public void setNrOfWindow(int nrOfWindows) { this.nrOfWindows = nrOfWindows } Setter method automatically: The rest of setter method for other properties can be set automatically as we did for getter methods, these are: Step-1: Right click in work space window Step-2: Choose Generate Step-3: Select Setter Step-4: Select the paropertis which are required in getter methods Step-5: Click OK It will automatically create the rest of setter method for you. You can see that the setter method works exactly like the constrcutor, which take argument from the object and set the object properties. Objects: Objects are the real world entities. When they are created a default constructor is created. It may be used to call parameterized constructor, and the getter or the setter metods Here is the complete example: In one file we create class House and 2nd contains the main method. The object is called from main method and the getter method is called from Syste.out.println(house.getNrOfDoors); class House package com.example; public class House { private int nrOfWindows; private int nrOfDoors; private String typeOfRoof; private String typeOfWall; public House() { nrOfWindows = 0; nrOfDoors =0; typeOfRoof =null; typeOfWall =null; } public House(int nrOfWindows, int nrOfDoors, String typeOfRoof, String typeOfWall) { this.nrOfWindows=nrOfWindows; this.nrOfDoors = nrOfDoors; this.typeOfRoof=typeOfRoof; this.typeOfWall = typeOfWall; } public int getNrOfWindows() { return nrOfWindows; } public int getNrOfDoors() { return nrOfDoors; } public void setNrOfWindows(int nrOfWindows) { this.nrOfWindows = nrOfWindows; } public void setNrOfDoors(int nrOfDoors) { this.nrOfDoors = nrOfDoors; } public void setTypeOfRoof(String typeOfRoof) { this.typeOfRoof = typeOfRoof; } public void setTypeOfWall(String typeOfWall) { this.typeOfWall = typeOfWall; } public String getTypeOfRoof() { return typeOfRoof; } public String getTypeOfWall() { return typeOfWall; } } main function package com.example; public class classAndObjects { public static void main(String[] args) { House house = new House() ; // create object to class House, this house variable will create memory location for House object System.out.println(“Nr of Doors are:”+house.getNrOfDoors()); } } Output Nr of Doors are:0 If now we call the setter method to set the nrOfDoors and the print as: public static void main(String[] args) { House house = new House() ; // create object to class House, this house variable will create memory location for House object house.setNrOfDoors(10); System.out.println(“Nr of Doors are:”+house.getNrOfDoors()); } The output will be Nr of Doors are:10 As a final step in main, let use set all values through constructor at object creation and set few of them using the setter method and finally print all properties. public class classAndObjects { public static void main(String[] args) { House house = new House(10, 2, “Tiles”, “Plaster”) ; // create object to class House, this house variable will create memory location for House object house.setNrOfDoors(4); house.setTypeOfWall(“Muddy”); System.out.println(“House Properties:”); System.out.println(“Nr of Windows are:”+house.getNrOfWindows()); System.out.println(“Nr of Doors are:”+house.getNrOfDoors()); System.out.println(“Nr of Doors are:”+house.getTypeOfRoof()); System.out.println(“Nr of Doors are:”+house.getTypeOfWall()); } } The output Now will be: House Properties: Nr of Windows are:10 Nr of Doors are:4 Nr of Doors are:Tiles Nr of Doors are:Muddy Share on Facebook Share Send email Mail Print Print