Java-Specific Language Features Flashcards
prep code
A form of pseudocode, to help you focus on the logic without stressing about syntax
- Instance variable declarations
- Method declarations
- Method logic
Testing code
A class or methods that will test the real code and validate that it’s doing the right thing
Real code
The actual implementation of the class.
Coding strategy
- Figure out what the class is supposed to do
- List the instance variables and method
- Write prepcode for the methods
- Write test code for the methods
- Implement the class
- Test the methods
- Debug and reimplement as needed
- Express gratitude that we don’t have to test our learning experience app on actual live users
What happens in Integer.parseInt() if the thing you pass isn’t a number? And does it recognize spelled-out numbers, like “three”?
Integer.parseInt() works only on Strings that represent the ascii values for digits (0, 1, 2,3, 4, 5, 6, 7, 8, 9). If you try to parse something like “two”, the code will blow up at runtime
Difference between for and while
A while loop has only the boolean test; it doesn’t have a built-in initialization or iteration.
If you know how many times to loop, a for loop is cleaner
Enhance for loop
Easier to iterate over all the elements in an array
Casting primitives
A long is bigger than an int and the compiler can’t be sure where that long has been
ArrayList vs. Array
- A plain old array has to know its size at the time it’s created
- Arrays use array syntax that’ s not used anywhere else in Java
- To put an object in a regular array, you must assign it to a specific location
Non Short Circuit Operators (&, |)
When used in boolean expressions, the & and | operators act like their && and “” counterparts, except that they force the JVM to always check both sides of the expression
Package
- Help the overall organization of a project or library
- Give you a name-scoping, to help prevent collisions if you and 12 other programmers in your company all decide to make a class with the same name
- Provide a level of security, because you can restrict the code you write so that only other classes in the same package can access it
How does a full-name really help package? What’s to prevent two people from giving a class the same package name?
Java has a naming convention that usually prevents this from happening, as long as developers adhere to it
Does import make my class bigger? Does it actually compile the imported class or package into my code?
- Import only saves you from typing
2. Import is simply the way you give Java the full name of a class
How come I never had to import the String class?
Because java.lang package sort of “pre-imported” for free
Inheritance
- The subclass inherits from the superclass
2. Subclass extends the superclass
Subclass
- The subclass can add new methods and instance variables of its own, and it can override the methods it inherits from the superclass
- A subclass can only extends from one superclass
Why isn’t Instance variable overriden
Because they don’t need to be. They don’t define any special behaviors, so a subclass can give an inherited instance variable any value it chooses
What if the superclass wants to use the subclass version of the method?
No, there’s no sort of reverse or backward inheritance
What if I want to use BOTH the superclass version and my overriding subclass version of a method?
we can do so using super.method(), which calls the inherited version, then comes back to do your own subclass-specific code
Are there any practical limits on the levels of subclassing? How deep can you go?
Most are no more than one or two levels deep. It usually makes more sense to keep your inheritance trees shallow
Can you make a method final, without making the whole class final?
If you want to protect a specific method from being overridden, mark the method with the final modifier.
Mark the whole class as final if you want to guarantee that none of the methods in that class will ever be overridden
Why would you ever want to make a final class?
A final class prevent from overriden
Overload method
A different method that happens to have the same method name. It has nothing to do with inheritance and polymorphism
Concrete class
Those that are specific enough to be instantiated
Abstract method
- No use, no value, no purpose in life, unless it is extended
- Must be overridden
- Implementing an abstract method is just like overriding a method
Abstract class
- You can mix both abstract and non-abstract methods in the abstract class
Abstract vs. Inheritance
Abstract method is that even though you haven’t put in any actual method, you’ve still defined part of the protocol for a group of subtypes
Is class Object abstract?
No. Object is a non-abstract class because it’s got method implementation code that all class can inherit and use out-of-the-box, without having to override the mehtods
Compiler
Check the class of the reference type-not the object type-to see if you can call a method using that reference
Polymorphism
Many form
Interface
- Solve multiple inheritance problem by giving you much of the polymorphic benefits of multiple inheritance without the pain
- A class can implement multiple interfaces
- A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract
Super
The keyword super lets you invoke a superclass version of an overridden method, from within the subclass
Stack
- Where method invocations and local variables live
2. The method on the top of the stack is always the currently executing method
Heap
Where all objects live
Instance variables
Declared inside a class but no inside a method. They represent the “fields” that each individual object has
Local variables
Declared inside a method, including method parameters. They’re temporary, and live only as long as the method is on the stack
Local variables
Declared inside a method, including method parameters. They’re temporary, and live only as long as the method is on the stack
Constructor
The code that runs when you instantiate an object
Are constructors inherited? If you don’t provide a constructor but your superclass does do you get the superclass constructor instead of the default?
No. Constructors are not inherited. We’ll look at that in just a few pages
“this” method
- Use this() to call a constructor from another overloaded constructor in the same class
- A constructor can have a call to super() OR this(), not both
- this() can be used only in a constructor
“this” method
- Use this() to call a constructor from another overloaded constructor in the same class
- A constructor can have a call to super() OR this(), not both
- this() can be used only in a constructor
Reference variables
Can be used only when it’s in scope