mode 4 Flashcards
What is a String?
- -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”
Equivalency of String in Java: are we checking the memory address or the content?
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
What is Stringbuilder and StringBuffer and what are their differences?
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
What makes a string immutable?
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
Why is a string immutable?
The answer is for efficiency
What is Abstraction?
Abstraction - revealing WHAT something does, not HOW it does that something
Similarities between Abstract Classes (AC) and Concrete Classes
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
Differences between Abstract Classes (AC) and Interfaces
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
Why would we use Abstract Classes over Interface ?
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
Using Abstract classes w/ methods:
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
Using Abstract classes w/ static methods and variables
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
Using Abstract classes w/ constructors and initializer blocks
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
What is an interface?
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 can we use Interface on a class?
To utilize an interface on a class…you must use the keyword “implements”
Note, interfaces EXTENDS other interfaces (not implements)
Can Interface implements multiple interfaces?
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 {