OOP - Object & Class Flashcards

1
Q

Class

DEFINE

A

it is a template where many instances (objects) can be instantiated

A.K.A. blueprint & prototype

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

Object

DEFINE

A

It is an instance of class (object of a class)

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

HOW TO CREATE A CLASS

A
SYNTAX:
-class keyword helps us to create a new class
-class in Java can only be public or default
-it CANNOT be protected or private
accessModifier class ClassName{
}
class Apple{
//class members
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

HOW TO CREATE AN OBJECT

A

SYNTAX:
-new keyword is MUST to use to create an object
-without new keyword, you cannot create a new object
-new keyword is always used together with contructor to create a new object
ClassName variableName = new ConstructorName();
Apple apple1 = new Apple();
Apple apple2 = new Apple();
Scanner scan = new Scanner(System.in);

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

Constructors:

A
  • Helps us create objects of a class
  • It is always used next to “new” keyword (helps to instantiate an object)
  • it can be default, or 1 arg, or more args depending on the need
  • Once you create your own constructor, Java will no longer provide the default.
  • it can be overloaded similar to methods
  • Syntax is similar to methods except it doesn’t have a return type or void type
  • It should always be named as the ClassName
  • Whenever it is invoked it runs a block of code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

OOP principles

so far

A
  • Software is created to resolve real life problems
  • Programming languages can be used to create software
  • Object - oriented is simply known as an approach to programming that breaks a programming problem into objects
  • Different Java objects can interact with one another
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method Overloading and Overriding

A
-Method overloading happens in the same class and it is known as having multiple methods 
with the same name but different arguments. Constructors can also be overloaded with 
different arguments similar to methods.
-Method overriding happens in the child class and this is known as child class implementing a 
different method body for the parent class method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

“extends” keyword

A

-Creates child parent relationship

public class Car{
   public void honks(){
      System.out.println("This car honks");
    }
}
public class Mercedes extends Car{ // Inheritance

public void honks(){
System.out.println(“Mercedes honks”);
}
}

Mercedes becomes a child class
Car becomes a parent to Mercedes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Class Members

A
1. Instance variables: defines the attributes of the instance
   These are declared in the class level
  1. Methods/functions: defines the behaviors of the instances
    - void methods
    - return type methods

3.Constructor: helps to instantiate the class with instances (helps create objects)

  1. Blocks: used to initialize static and non-static instance variables
    - instance blocks
    - static blocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Instance variables vs local variables

A

Local variables:

  • They are variables that are declared in: main, if-else, switch, loops, methods, etc.
  • Their scope is restricted to the statements or methods they are declared in

Instance variables:

  • They are declared in the class level and belongs to the instances of that class.
  • Their scope is wider than local variables and determined by access modifiers (public, protected, default, and private)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Final and static keywords

A

Non-Access Modifiers

Static instance variables:
-static instance variables means the variable belongs to the class and hence can be invoked with class name

public static int noOfAnimals = 0;

-non-static instance variables belong to the objects and they can be called with an object name

public int noOfAnimals = 0;

Final instance variables:

  • They must be initialized meaning you must assign them some values
  • This default value that you initialized CANNOT BE CHANGED anywhere

public final int noOfAnimals = 0;

NOTE; One instance variable can be final and static at the same time known as CONSTANTS
examples:
pi = 3.14
daysInAWeek = 7
monthsInAYear = 12

public static final int noOfAnimals = 0;

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

Blocks

A
  1. Local blocks: Switch, if-else, loops, methods, constructors
Class Members
2. Instance blocks: used to initialize non-static instance variables
 {
         hasEyes = true;
}
3. Static blocks: used to initialize static instance variables
   static{
         hasEyes = true;
}

NOTE: static instance variables can be initialized in the non-static blocks as well.
NOTE: non-static instance variables can only be initialized in the non-static blocks

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

Can you run a code before main method in Java?

A

Yes, we can have static blocks which can run before main method

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

HOW TO CREATE A CLASS

A

SYNTAX:

  • class keyword helps us to create a new class
  • class in Java can only be public or default
  • it CANNOT be protected or private
accessModifier class ClassName{
}
class Apple{
//class members
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Instance Variables

OTHER NAMES

A

attributes - fields - instance variables - states

-WHAT DOES THE OBJECT HAVE-

defines the attributes of the instance

declared in the class level

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

Methods

OTHER NAMES

A

methods - functions - behaviors

-WHAT DOES THE OBJECT DO-

drives(), stops(), signals(), brakes(), accelerates(), honks()

17
Q

HOW TO CREATE AN OBJECT

A

SYNTAX:

  • new keyword is MUST to use to create an object
  • without new keyword, you cannot create a new object
  • “new” keyword is always used together with constructor to create a new object

ClassName variableName = new ConstructorName();

Apple apple1 = new Apple();
Apple apple2 = new Apple();
18
Q

Java is an OOP Language

A

Object-Oriented Programming

Java has an Object class that is the parent to any class by default

The core concept of the object-oriented approach is to break complex problems into smaller
objects.

So, Object-oriented simply known as an approach to programming that breaks a
programming problem into objects

19
Q

Constant

A

Instance variable that is final and static at the same time

20
Q

WHEN DO STATIC AND INSTANCE BLOCKS INVOKE?

A

Once you create an object both static and instance blocks will be executed.

NOTE:
Static block will be executed once and first.
Instance block will be executed for each object being created

EXECUTION ORDER

SAME CLASS DIFFERENT CLASS

  1. static block 1. main method (top to bottom)
  2. main method (top to bottom) 2. static block will be executed
  3. Instance block, for each object 3. Instance block, for each object
21
Q

Can you run a code before main method in Java?

A

-Yes, we can have static blocks which runs before main method

22
Q

“this” keyword

A
  • this keyword is useful when we have instance variable and method parameter with same name
  • this keyword is a reference to the current object of its type
23
Q

returnType

A

It specifies what type of value a method returns.
For example, if a method has anintreturn type then it returns an integer value

If the method does not return a value, its return type isvoid

24
Q

methodName

A

It is an identifierthat is used to refer to the method in a program, could be any name but should be meaningful to increase readability of your code

25
Q

method body

A

It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces{ }

26
Q

method parameters

A

Arguments that we pass inside the method parentheses

27
Q

Methods are functions or behaviors of the class

FACT

A
28
Q

An instance is a single occurrence of a class

FACT

A