Sunday, April 14, 2019

what are classes and how we can declare classes in Java

 what are classes and how we can declare classes in Java

Hello guys welcome to the next my Blogger on Java programming for beginners till now we have seen the procedural part of Java now we will see some of the object-oriented aspects of Java and first of all we will see what are classes and how we can declare classes in Java.
We can use categories in Java thus 1st of all
What is a class
It contains variables and methods. Usually (but not always) category used as templet for making objects. Class is structure unit of Java program. In other words, Java application consists of classes.
We may have noticed that whenever we have created our first program it was a class and inside this class we were calling this main method so every Java project has a minimum of one category and one main technique and sophistication is largely an information structure or kind in aspect that you'll be able to outline some variables that ar known as member variables and methods which are called member methods and the most important property of classes are you can create instance or object from class so you can maintain different states of at last using these instances or objects so let us see how we can define a class which will be a new class and how we can use it with our main category that is my category thus as an example we would like create|to form|to create} thusme student arrangement and for that we are going to make a category so simply go to your project if you are using eclipse and go to that package inside which you will be having a class my class in my case just go to the package right click a similar package and visit new and make a brand new category and this category name we are going to name it as student okay and leave everything as default and click finish and now we have package so we have two classes now my class is the first class which we were working on and the second class is called student dot Java and this is often be} the second category we've got created currently currently this category will have its member variables and strategies thus as an example what can be the traits of the student student can have his ID so ID is integer values I declare a variable called ID student can have name so I declare a variable called name and student can have age his age so I have declare student age okay so this is basically you have created a class and it can have member variables it can also have member methods that we will see in a moment but let's consider this is a basic student class which contains three variables ID name and age okay now to call this class in your main method in my my class you can just create an instance of or object of the class so how you can bang you'll be able to simply decision a similar name student category name simply copy this name here {and then|then|so|and thus} provide the name to your instance so as we have a tendency to have we give any name to our variables for example age name or something here also you can give the name to the students variable for example the name is mark we have a tendency to ar creating the info structure for mark is up to new student okay associated this primarily we've got created an instance of student category that we have a tendency to named as mark so this is a mark instance so mark here mark here is called object okay or some people also call it as instense okay instance so mark is here called object or instance and this object we can use to you know assign values to the member variables of the student class for example marks ID is so whenever you call this instance called mark and whenever you use this dot separator it will give you the options which is provided by eclipse to you so the student class contain age ID and name thus we are going to select ID student ID is up to as an example a 1 mark is our 1st student and within the same method you can call mark dot name should be mark dot name is equal to name is mark we already know and the age so mark dot age is equal to and he is fifteen year old for example so what we have done is we have created an instance of student class and we name it as mark and then to this instance we have created or we have assigned some values and these values we can you know access these values with the same name small dot ID mark dot name and math dot age so you can just write this Oh control shift and then you can just write mark dot name is so you use plus or concatenation operator is {and then|then|so|and thus} and so mark dot age so we are going to print mark is no matter his age is years recent okay years recent okay and after we run this program it will print mark is 15 years old okay in a similar way we can create different instances of our classes and so what we can do is we can will be able to simply copy a similar mark student category {and we have a tendency to|and that we have a tendency to} will produce a brand new object known as this point we decision it Tom okay thus here tom is that the second instance of the scholar category okay and his ID is to call is Tommyhis ID is to name is Tommy itself and the age is for example 14 okay so in here we can once again make the copy of this print line method and this time we will just replace mark by tom and now you can see it prints tom is 14 year old and is there something wrong here yeah here we have not modified this instance name here once more we'll run the program and say is mark is fifteen year recent and tom is fourteen years recent okay so employing a category will|we will|we square measure able to} outline completely different object and that we can outline these object values of object range variable values that are freelance of every alternative thus this instance values which are ID Tom's ID Tom's name and Tom's age is independent of Mark's ID Mark's name and marks age okay so this is very important property of object that it can store its own state and it's independent of the other instance right now in you know in java programming or programming in general we never use or this kind of usage direct usage of variables are discouraged and why it's discouraged because any other person or whoever is whoever is using your code can modify these values so we use some you know some methods or some functions which can get or set these values okay so what I mean by getting and setting these values is I can go to this class called student and in here I can just use I can just go to the source tab and in here I can go to so we will go to the source and just go to generate getters and setters here and here we can see operation is not applicable in current plus okay and in here we will just use this and click once again getters and setters so just click one variable name or variable then only this will work so go to source once again and create getters and setters and in here select all so we are basically making getter and setter method for every value because we do not want to use or we do not want to access these variables directly we just create getters and setters for all of them and whenever we click OK you can see here Java has created us these Gators and setters for all these values ok and what this is basically doing is it has we whenever we want to you know assign some value to the ID we will use set ID method and whenever we want to get the value of ID we will use get method and this is called encapsulation of a variable and this is important because we do not want to give access to our variables to some other user which is unknown to us right and so now what we are going to do is we have generated these getters and setters in between our variables so we can just cut these values and paste it here and these Caterson settles we've got generated for all the worths right thus currently after we need to assign some value to the mark instance rather than this mark dot ID we will just use mark dot set ID was set ID and then we can give any ID to mark and this value assignment is we are not going to use in the same way we will use marks dot set name and this name we will give to mark and in the same way we will assign age to mark thus mark dot set age okay AJ is fifteen right and in a very same approach we'll do one thing with Tom's instance conjointly thus tom dot set ID tom dot set name so tom dot set name we will assign this name to Tom and Tom dot set age same thing here so basically what we have done we have used instead of direct assignment we are assigning values using these skaters and setters and this protects your data from corrupting if you somebody wants to corrupt or your daughter or something like this okay and to get these value you just call mark dot get name and mark dot get age okay and same we will do with our Tom instance and whenever we run the program result will be same but this is more preferred while you are accessing or assigning the values to the member variables thus this can be however categories works in Java.

No comments:

Post a Comment