mode 2 Flashcards
What is a constructor?
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.
Types of constructors
default constructor
No args constructor: implicit derived constructor when none given.
args constructor
What is an initialization block?
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
What is static initializer block
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
What is scope in Java?
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.
What are the scopes in java?
- -class scope (aka static scope)
- -object scope (aka instance scope)
- -method scope
- -block scope
What is the order of effect in our program?
Order of “effect”:
- static variables/methods (runs ONCE the first time the class is needed)
- static initializer blocks (runs ONCE the first time the class is needed)
- instance variables (runs EVERY TIME an instance of this class is created)
- instance initializer blocks (runs EVERY TIME an instance of this class is created)
- constructors
Where can the static keyword be used?
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.
Can we make a static variable inside a static method?
No!
Inheritance: What is an “is a” relationship vs a “has a” relationship?
-“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
What is the inheritance heirachy
Our Hierarchy
- Object
- |
- Animal
- / \
- Monkey Turtle
- / \
- Chimp ChimpSibling
Name a few types of polymorphism
There are many types of polymorphism
* -overriding * -overloading is compile time polymorphism * -upcasting is polymorphism * -downcasting is polymorphism
What is upcasting & downcasting in java
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;
what is overloading & overriding?
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
Why is upcasting and downcasting useful?
It helps us achieve modularity.
It enables us to decouple our code
What is tightly coupled code & decoupling ?
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.
Can we override a method in Java? a variable in Java?
Yes we can override a method.
No we can’t override a variable.
What is shadowing
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
CAN I override a STATIC method?
CAN I override an initializer block?
CAN I override a constructor?
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
What is the first line of the constuctor?
The super() keyword. It is implicitly written but can be explicitly written out too.
What does the super() keyword do?
It calls to the direct parent constructor and gives access to parent constructor, methods, fields.
Can we use super() to access a parent class members from outside of the object?
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();
Does Java support multiple inheritance?
No, this is because the child can only point to one parent in the constructor.
this vs super. what is the difference?
“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
What is the override and deprecated notation
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.
difference between this()/super() and this./super.
for this()/super() we make call to constructor
for this./super. we make call to only the members