Junior — Middle
Can you list the main properties or characteristics that classes can have?
sobes.tech AI
Answer from AI
The main properties (characteristics) of classes in Java include:
- Fields (class variables) — store the state of the object.
- Methods — define the behavior of the class and its objects.
- Constructors — special methods for creating class objects.
- Access modifiers (public, private, protected) — determine the visibility level of class members.
- Inheritance — a class can inherit properties and methods from another class.
- Encapsulation — hiding internal implementation and providing access through methods.
- Polymorphism — the ability to use objects of different classes through a common interface.
- Static members — belong to the class itself rather than to objects.
- Nested classes — classes declared within other classes.
Example of a simple class with some properties:
public class Person {
private String name; // field
private int age;
public Person(String name, int age) { // constructor
this.name = name;
this.age = age;
}
public String getName() { // method
return name;
}
public void setName(String name) {
this.name = name;
}
}