Object Oriented Programming Flashcards

1
Q

How to automate the creation of Getter and Setter?

A

IntelliJ –> Code –> Generate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the benefits of encapsulation?

A
  • Data hiding
  • Increased Flexibility
  • Reusability (Easy to reuse)
  • Testing code is easy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Class

A

xxx

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Object / Instance

A

xxx

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

recursive call

A

the method would call itself forever until the memory is full

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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));
   }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Composition

A
  • a way to design or implement the “has-a” relationship
  • the relationship is possible when one object contains another object and that object is fully dependent on it
  • in composition, we use an instance variable that refers to another object
  • to ensure the code reusability in the program
17
Q

Polymorphism

A
  • same name, different behavior
  • the ability of an object to take on many forms
  • it allows us to perform a single action in different behavior in different situations
  • the most common use of it in OOP occurs when a parent class reference is used to refer to a child class object
18
Q

What are the four basic concepts of OOP?

A
  • Inheritance
  • Abstraction
  • Polymorphism
  • Encapsulation
19
Q

getSimpleName()

A
  • the method returns the simple name of this class in the form of String
  • used to get the simple name of this class, as given in the source code
20
Q

static field vs. non-static field

A

static field

  • a static field belongs to the class
  • dont’ need an instance of that class to access its static field

non-static field

  • non-static fields are located in the instances of the class.
  • each instance of the class can have its own values for these fields
  • you need to have an instance of the class from which you can access it