Java OOP Flashcards
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
What are Classes and Objects?
So, a class is a template for objects,/like an object constructor and an object is an instance of a class.
How do you create an object (of Main)?
To create an object of Main, specify the class name, followed by the object name, and use the keyword new:
Can you access an object of a class in another class?
Yes!
This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)).
What is another name for a variable ?
an attribute of the class
What is another term for class attributes?
Another term for class attributes is fields.
How do we access attributes of a class?
You can access attributes by creating an object of the class, and by using the dot syntax (.):
The following example will create an object of the Main class, with the name myObj. We use the x attribute on the object to print its value:
How would you modify an attribute?
If you don’t want the ability to override existing values, what would you do?
declare the attribute as final:
The final keyword is useful when you want a variable to always store the same value, like PI (3.14159…).
The final keyword is called a “modifier”.
If you create multiple objects of one class, how would changing the attribute value in one object affect the other?
you can change the attribute values in one object, without affecting the attribute values in the other:
What are methods in Java and why use them?
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.
How do you create a method?
A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also create your own methods to perform certain actions:
myMethod() is the name of the method
static means that the method belongs to the Main class and not an object of the Main class. You will learn more about objects and how to access methods through objects later in this tutorial.
void means that this method does not have a return value.
How do you Call a Method?
To call a method in Java, write the method’s name followed by two parentheses () and a semicolon;
In the following example, myMethod() is used to print a text (the action), when it is called:
What is void and what can you replace it with?
The void keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method:
What is method overloading?
With method overloading, multiple methods can have the same name with different parameters:
Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.