+ 6
Guys how best can i understand methods classes and attributes in java
need help
2 odpowiedzi
+ 4
class is a blueprint of an object it defines its qualities (attributes) and behavior (methods).
To create an object in your progra you refere to its class calling the constructor.
You can look at it this way:if your objects are cookies, you can consider their class being the cookie cutter. Credit to Daniel Shiffman
https://m.youtube.com/watch?v=YcbcfkLzgvs
+ 1
An object has attributes and behaviors.
The word "object" usually includes both classes and instances, and also means a concept itself.
A class is a template.
A instance is a substance.
For example, human is a class.
Then we are instances of a human class.
A class defines attributes and instances should have own ones.
Let human has some attributes such as a name and an age.
So You have your own name and age as well as I do.
A class also defines some behaviors and instances should be able to do so.
It is important, that different attributes may change results of the same behavior among instances.
Let human to be able to tell one's name.
You can tell your name.
I can tell my name.
But they may sound differently unless we coincidentally have the same name.
In Java, attributes are coded as fields and behavior as methods.
Class defines fields and methods.
An instance is created with their own fields and methods by the ketword "new".
class Human {
String name; // a field
int age; // a field
// creates a new instance of Human.
// called with "new"
public Human(String name, int age) {
this.name = name;
this.age = age;
}
// tells one's name.
// may sounds differently according to name.
public void tellName() {
System.out.println("I'm " + this.name + ".");
}
}