Java Programming Features Flashcards
Help me to become familiar with what to include in the IA
What is OOP?
Object-Orientated Programming. This is a way of programming based on the concept of a collection of objects containing both data and methods (functions) coming together to make a program.
How do objects interact in OOP?
Objects interact with each other by accessing data and calling methods from each other.
Why do we have OOP?
To avoid “spaghetti code”. The opposite of OOP is procedural programming which basically uses a single class to write the whole programming meaning the repetitive copy pasting of functions/methods and too much interdependency.
Components of an object
Data - called “properties”
Functions - called “methods”
Objects are _______ of classes
instances
What are classes and why are they important to OOP?
A class is essentially a blueprint for creating objects. Objects are instances of classes that have specific attributes and behaviors. A class defines the properties and methods common to all objects of that type.
Difference between variable, parameter and property?
Variable:
A container that is used to temporarily hold a data value. They are declared with a specific data type and can hold different values throughout the execution of the program. (e.g. int age = 7)
Parameter:
That shit within the brackets of a method. It represents the new value that is going to be assigned to a property in that class.
(e.g. public void setName(String newName))
Property:
Properties are attributes or characteristics of objects within a class. Usually they are represented using private instance variables. (e.g. private String name;)
what does static mean when you are making a method?
It means that you do not have to make an object* when you’re calling it.
*Making the object means for example Student s = new Student();
What is encapsulation?
This is when we use private instances and getters and setters in order to make sensitive data hidden from users and increase the amount of abstraction.
What is polymorphism?
Polymorphism literally means “many forms”
Essentially, this is when we can have multiple methods with the same name (hence, many forms) but they must have different parameters.
Additionally, another important form of polymorphism is when we can extend classes with another class allowing one class to inherit properties and methods from another class this is called inheritance by the way and is also another Java feature).
What are inner classes?
This is when we next classes. The purpose of this is to group classes that belong together which makes our code more readable and maintainable. To do this in our main method, we create an object of the outer class, and then create another object of the inner class (the inner class would be declared within the outer class by the way)
What are constructors and why do we have them?
Constructors are essentially the method which creates the object for us. They will have the same name as the name of the class. We can use constructors to set the initial values for object attributes which helps us to avoid repetitive code thus increasing the elegance.
There is a basic, hidden constructor in each class that does the actual creation but we can multiple constructors in a class. For example,
they can take parameters which is used to create the attributes we want for that object.
Constructors cannot have return types like void.
Example of constructor:
public class Main {
int x; // Create a class attribute
// Create a class constructor for the Main class
public Main() {
x = 5; // Set the initial value for the class attribute x
}