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