mode 2 Flashcards

1
Q

What is a constructor?

A

A constructor is a special type of method that is used to initialize an object during its construction.

A constructor has the same name as the class and DOESN’T have a return type.

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

Types of constructors

A

default constructor

No args constructor: implicit derived constructor when none given.

args constructor

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

What is an initialization block?

A

An initializer block is basically a method without a name, so when java sees the block it will call the block IMMEDIATELY because it knows that you won’t ever be able to refer to the block again.

The initializer block will be called every time a class is instantiated

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

What is static initializer block

A

The static initializer block is a type of initializer block that will run ONCE the FIRST time anyone calls its class for anything

It will NOT run for EVERY instance created

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

What is scope in Java?

A

The area where a variable exists. Outside of this area the variable does not exist.

This is NOT to be confused with access levels…that happen to determine who has access to a variable when it DOES exist.

It deals with permissions, not existence.

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

What are the scopes in java?

A
  • -class scope (aka static scope)
    • -object scope (aka instance scope)
    • -method scope
    • -block scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the order of effect in our program?

A

Order of “effect”:

  1. static variables/methods (runs ONCE the first time the class is needed)
  2. static initializer blocks (runs ONCE the first time the class is needed)
  3. instance variables (runs EVERY TIME an instance of this class is created)
  4. instance initializer blocks (runs EVERY TIME an instance of this class is created)
  5. constructors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Where can the static keyword be used?

A

The static keyword can be used on:

  • variables
  • methods
  • classes (only inner classes)
  • interfaces (only inner interfaces)
  • initializer blocks

It can’t be used on constructors though.

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

Can we make a static variable inside a static method?

A

No!

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

Inheritance: What is an “is a” relationship vs a “has a” relationship?

A

-“is a” means you ARE a type of the specific object;
it’s like living a double life, where you have two identities

-“has a” means you CONTAIN a type of the specific class;
it’s like owning a car…you are NOT a car…you just own one

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

What is the inheritance heirachy

A

Our Hierarchy

  • Object
  • |
  • Animal
  • / \
  • Monkey Turtle
  • / \
  • Chimp ChimpSibling
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name a few types of polymorphism

A

There are many types of polymorphism

	 * -overriding
	 * -overloading is compile time polymorphism
	 * -upcasting is polymorphism
	 * -downcasting is polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is upcasting & downcasting in java

A

Upcasting is the typecasting of a child object to a parent object

Implicit upcasting:
Animal myAniChimp = new Chimp();
Explicity upcasting:
Chimp myChimp = new Chimp();
Animal myAnimalTwo = (Animal)myChimp;

Downcasting means the typecasting of a parent object to a child object
                Animal myAnimalFour = new Chimp();
		Chimp myChimpFour = (Chimp)myAnimalFour;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is overloading & overriding?

A

Overloading: a feature of Java in which a class has more than one method of the same name and their parameters are different

Overriding: A child class creating a method with the SAME NAME (and parameter list) as a method in the parent class; this results in the behavior/functionality being altered

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

Why is upcasting and downcasting useful?

A

It helps us achieve modularity.

It enables us to decouple our code

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

What is tightly coupled code & decoupling ?

A

Tightly coupled code or spaghetti code is code that is written in such a way where small changes to one file creates a wave of changes many other source code files.

17
Q

Can we override a method in Java? a variable in Java?

A

Yes we can override a method.

No we can’t override a variable.

18
Q

What is shadowing

A

In java when a child class has a variable with the same name as a variable in the parent class….then we SHADOW the parent’s variable.

This is a concept called shadowing; not overriding

19
Q

CAN I override a STATIC method?

CAN I override an initializer block?

CAN I override a constructor?

A

No, we can’t override a static method, but we can shadow it.

No, we can’t override an initializer block? it doesn’t make sense to

No, we can’t override a constructor? it doesn’t make sense to

20
Q

What is the first line of the constuctor?

A

The super() keyword. It is implicitly written but can be explicitly written out too.

21
Q

What does the super() keyword do?

A

It calls to the direct parent constructor and gives access to parent constructor, methods, fields.

22
Q

Can we use super() to access a parent class members from outside of the object?

A

No we can’t. In order to access parent class members from outside of the object, we use upcasting.

To access from inside a class, we use super();

23
Q

Does Java support multiple inheritance?

A

No, this is because the child can only point to one parent in the constructor.

24
Q

this vs super. what is the difference?

A

“this” is a reference to the current object we’re currently inside of(specifically to its instance & static members)

“super” is a reference to the current objects direct parent class.

both this and super can’t exist at the same time

with BOTH the “this” and “super” keywords we can access fields(aka state variable), methods, and constructors

25
Q

What is the override and deprecated notation

A

The override annotation is for clarity/readability AND it ensures that there IS a method in the parent class that you are overriding (it protects you from misspelling the method name)

The deprecated annotation lets the developers know that this functionality will no longer be supported in the future. IT STILL DOES FUNCTION, but the developer should move to a new functionality that is designed to replace the existing deprecated functionality.

26
Q

difference between this()/super() and this./super.

A

for this()/super() we make call to constructor

for this./super. we make call to only the members