Java Classes Flashcards
What is object oriented programming?
object-oriented programming is about creating objects that contain both data and methods
What are classes in Java?
A Class is like an object constructor, or a “blueprint” for creating objects.
What is an object?
An object is an instance of a class. You would instantiate an object which would call the constructor of the object and then depending on the parameters involved you would set the variables and then have access to the methods.
What are Java Class Attributes?
Attributes are variables within a class and can be any of the data types
How would you access attributes in an object?
You can access attributes by creating an object of the class, and by using the dot syntax (.):
How do you modify an attribute?
By using the assignment operator = after the object is created
How does the keyword final affect an attribute in a class?
It makes it so you can’t modify the value.
How would you call a static method in a class?
Would simply call the method myStaticMethod(); in the main method
How would you call a non-static method?
Would need to first create an object and then use the dot notation to access the method.
What is a constructor in Java?
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes
What are constructor parameters?
Constructors can also take parameters, which is used to initialize attributes. Parameters are data that is passed into the object to set attributes or for method calls.
What are the two types of modifiers for methods in Java?
1- Access
2 - Non-access
What are the different access modifiers for classes, methods, and attributes?
For classes, you can use either public or default. For attributes, methods and constructors, you can use the one of the following
What are the different non-access modifiers for classes, methods, and attributes?
For classes, you can use either final or abstract. For attributes and methods, you can use the one of the following
What is a static method?
A static method means that it can be accessed without creating an object of the class, unlike public
What is an abstract method?
An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass
What is encapsulation?
he meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:
declare class variables/attributes as private provide public get and set methods to access and update the value of a private variable
What are getter and setter methods in java?
The get method returns the variable value, and the set method sets the value
Why is encapsulation important?
Better control of class attributes and methods
Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
Flexible: the programmer can change one part of the code without affecting other parts
Increased security of data
What is a package?
A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
What is the this keyword and how is it used?
It is used in the constructor to set the value of the variable in the class and to reference it.
What is the super keyword and how is it used?
It is used to call the constructor of the parent class or reference a variable or method of the parent class
What is the toString method and how is it created?
it is a method that is called to verify the contents of the variables defined in a class. It can be created automatically with most IDE’s