Object Oriented Programming Flashcards
1
Q
How to automate the creation of Getter and Setter?
A
IntelliJ –> Code –> Generate
2
Q
Constructors
A
for you to initialize the object that you are creating and do whatever else you wanna do at the time the object is being created.
- a special method that is used to initialize objects
- it is called when an object of a class is created
- it can be used to set initial values for object attributes
- it must have the same name as the class within which it is defined
3
Q
Constructor Chaining
A
- calling one constructor from another constructor
- it can be done using this() keyword for constructors in the same class
- you can also use super() keyword to call constructors from the base class
4
Q
Encapsulation
A
- refers to the bundling of data, along with the methods that operate on the data, into a single unit
- may also refer to a mechanism of restricting the direct access to some components of an object
- many programming languages use encapsulation frequently in the form of classes
5
Q
What are the benefits of encapsulation?
A
- Data hiding
- Increased Flexibility
- Reusability (Easy to reuse)
- Testing code is easy
6
Q
Reference variable
A
- reference type is used to point objects/values
- classes, interfaces, arrays, enumerators, and annotations are reference types in Java.
- reference variable can also store null values. By default, if no object is passed to a reference variable then it will store a null value
- You can access object members using a reference variable and dot syntax
7
Q
Class
A
xxx
8
Q
Object / Instance
A
xxx
9
Q
super keyword
A
- used to access/call the parent class members (variables and methods)
- can’t be used in static areas (the static block or a static method)
- Any attempt to do so will lead to compile-time errors
- is commonly used with method overriding
10
Q
this keyword
A
- used to access/call the current class members (variables and methods)
- this is required when we have a parameter with the same name as an instance variable (field)
- can’t be used in static areas (the static block or a static method)
- Any attempt to do so will lead to compile-time errors
- commonly used with constructors and setters
11
Q
Method Overriding
A
- if subclass (child class) has the same method as declared in the parent class
- in other words, if a subclass provides the specific implementation of the method that has been declared by one of its parent classes, it is known as method overriding.
Rules for Java Method Overriding
- the method must have the same name as in the parent class
- the method must have the same parameter as in the parent class
- there must be an IS-A relationship (inheritance)
12
Q
recursive call
A
the method would call itself forever until the memory is full
13
Q
What are the differences between a static method and an instance method?
A
Static Methods
- static methods are methods in Java that can be called without actually creating an object of that class
- it becomes part of the class (a class method: a method that belongs to a class rather than to an object)
- they are designed with the aim to be shared with all objects created within that class
- static methods can’t be overridden but can be overloaded
Instance Methods
- instance methods are methods that require an object of its class to be created before it can be called
- instance methods belong to the object of the class and not the class
- they can be overridden and overloaded
14
Q
How to pass objects as arguments?
A
code example
public double distance(Point obj) { int x = obj.getX(); int y = obj.getY(); return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y)); }
15
Q
Inheritance
A
- one class is allowed to inherit the features (fields and methods) of another class
- the keyword used for inheritance is extends
- the class whose features are inherited is known as superclass (base class or parent class)
- the class that inherits the other class is known as a subclass ( or a derived class, extended class, or child class)
- the subclass can add its own fields and methods in addition to the superclass fields and methods