mode 4 Flashcards

1
Q

What is a String?

A
  • -An object that contains an array of characters
  • -A String is immutable which means it can’t be changed
  • A String is sometimes called a “psuedo primitive”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Equivalency of String in Java: are we checking the memory address or the content?

A

We’re checking the memory address:
System.out.println(“comparison one: “ + (myStringTwo == “Donuts from Krispy Kreme”)); //prints false

Here we’re checking the CONTENTS of the objects
System.out.println(“comparison two: “ + myStringTwo.equals(“Donuts from Krispy Kreme”)); //prints true

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

What is Stringbuilder and StringBuffer and what are their differences?

A

STRING BUILDER
StringBuilder is essentially a mutable counterpart to the String class

STRING BUFFER
StringBuffer is essentially a mutable counterpart to the String class

The difference is that StringBuffer is thread safe

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

What makes a string immutable?

A

A String is immutable because of the combination of THREE facts:

	 * -All String modifying methods INTENTIONALLY NEVER alter the existing String, they instead create
	 * a new String and return a reference to that new String...leaving the original String unmodified
	 * -The class is final, so that NO ONE can EVER..create a child class and override the String's
	 * intended functionality
	 * -The char[]/byte[] inside String is final so you can NEVER point to a different array, to get around
	 * the immutability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is a string immutable?

A

The answer is for efficiency

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

What is Abstraction?

A

Abstraction - revealing WHAT something does, not HOW it does that something

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

Similarities between Abstract Classes (AC) and Concrete Classes

A

Abstract classes and Concrete class have more similarities than they have differences.
Some of the differences include:
-Abstract classes cannot be instantiated
-AC can have abstract methods
-an abstract method MUST be overridden eventually

 Beyond these differences, the inheritance works exactly the same as a concrete class

BOTH OF THESE MAY HAVE STATIC METHODS

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

Differences between Abstract Classes (AC) and Interfaces

A

Differences between Abstract Classes (AC) and Interfaces:

            interfaces                                              AC
       -------------------------                        --------------------------------
abstract method (until default)              concrete & abstract methods

implements keyword extends keyword

methods implicitly:
abstract & public no implicit modifiers

variables implicitly:
public, static, final

NO constructor YES constructor

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

Why would we use Abstract Classes over Interface ?

A

PRO ac: can provide concrete implementations (including methods and instance variables)
(java 8 added default….so the interface can now have concrete methods)
PRO ac: constructor
PRO i: can implements multiple interfaces without taking up the ONE spot you have to extend a class

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

Using Abstract classes w/ methods:

A

Abstract classes w/ methods:

  • -You CAN have abstract methods inside of an abstract class
  • -You CAN have concrete methods inside of an abstract class
  • -You CAN have an abstract class with ZERO abstract methods
  • -You can NOT have abstract methods inside of a concrete class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Using Abstract classes w/ static methods and variables

A

Abstract classes w/ static methods:

  • -You CAN have static methods in an abstract class
  • -You can NOT have abstract static methods

Abstract classes w/ variables:

  • -You CAN have variables in an abstract class
  • -You can NOT have abstract variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using Abstract classes w/ constructors and initializer blocks

A

Abstract classes w/ constructors:

  • -You CAN have a constructor in an abstract class
  • -You can NOT have an abstract constructor
  • Abstract classes w/ initializer blocks:
  • -You CAN have an initializer block in an abstract class
  • -You can NOT have an abstract initializer block
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an interface?

A

An interface is a CONTRACT between itself and any implementing classes

Just like a real world contract, the implementing class needs to fulfill the contract’s requirements

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

How can we use Interface on a class?

A

To utilize an interface on a class…you must use the keyword “implements”

Note, interfaces EXTENDS other interfaces (not implements)

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

Can Interface implements multiple interfaces?

A

Java does NOT support multiple inheritance so you can’t extends multiple classes,
However, interfaces are NOT parent classes, they are contracts. So yes, you can implements multiple interfaces

public interface Edible extends Freezable , Heatable {

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

What are the implicit values of variables and methods in interface

A
Variables in an interface are IMPLICITLY public & static & final   
/* public static final */ String myString= "hello interface world";
Methods in an interface are IMPLICITLY public & abstract
/* public abstract */ void breakDownIntoNutrients();
17
Q

Can Interfaces have Constructor, INITIALIZER BLOCKS, or Static INITIALIZER BLOCKS

A

NO!

18
Q

Can we have default and concrete static methods in an interface?

A

default void myDefaultMethodInAnInterface() {
System.out.println(“yes, we CAN have an implementation of a method in an interface”);
System.out.println(“using a default method”);
}

public static void myStaticMethodInAnInterface() {
	System.out.println("yes, we CAN have an implementation of a method in an interface");
	System.out.println("using a static method");
19
Q

Can we use the abstract method from an abstract class? If yes, how?

A

Yes. In order to access abstract classes, the child class MUST override any abstract methods before the object can be concrete.

If the child class is itself an abstract class then your child DOESN’T need to override this method….instead your concrete GRANDCHILD must override the method

(While your abstract child isn’t FORCED to override your abstract methods, it CAN to save your grandchild the headache of overriding)

20
Q

Can I have a private abstract method?

A
No, private means ONLY THIS CLASS can access
//	private abstract void myPrivateAbstractMethod();
21
Q

Can I have a final abstract method?

A
No, final means it can't be overridden, abstract means it needs to be overridden
//	public final abstract void finalAbstractMethod();
22
Q

Can I have a final abstract class or final interface?

A

NO!

23
Q

Can I extends multiple classes?

A

Java does NOT support multiple inheritance so you can’t extends multiple classes,

24
Q

What is a Marker Interface?

A

An interface with no abstract methods. E.g: serializable

25
Q

What is a Functional Interface?

A

An interface with one abstract method. E.g comparator