+ 2
Simple explanation of Object in java
I'm really having a hard time trying to understand what an Object is. Can someone please explain it to me like you would to a 3 year old please.
4 odpowiedzi
0
Hi,
The Objects are hold different data.
Example if you need store many Person's date you cannot declarate lot of variable like this:
int age1;
int age2;
....
You need to wrapped the age var in a class
class Person{
int age;
String name;
}
In this example you create a class, and you can use this class to create more people:
Person p = new Person();
Person p1 = new Person();
each object (p, p1) are hold age, name variable
So you can wrapping a variable collection in a class.
Every class is an Object, every class is extend the Object
I hope you will understand few things
+ 1
Main goal of our program is to do something it may be any task for example mathematical calculations, or storing the data, sorting the data etc etc
Take mathematical operation as an example for simple understanding
We have various mathematical operations like add subtract etc etc
Unfortunately In your code there is also string related methods
You decided to separate the string string related operation to one group and mathematical related to one group
Here you need a class to group the methods
Class holds the methods and your data
Class math{
Variable a;
Variable b;
constructor(){}
Add method(){}
sub method(){}
}
here you grouped the things successfully
How will you access those???
Here we need the thing called as OBJECT
math yourobject=new math();
In Java we use the new keyword to create something and to allocate memory for that
You have successfully created object
How will you access class methods???
Simply you have to use. (. -dotopeator)
yourobject.yourmethod(your values)
0
https://code.sololearn.com/c6MZ5n1HZwAu/?ref=app
Here is an example for classes, and object
0
Above one is just a rough explanation