Java Flashcards
What are the 3 uses of the keyword final
variable
method
class
Can classes marked as final be extended?
no, that is the purpose of marking a class as final
Does a final class mean the objects of that class are immutable?
No, you can change the fields in.,
Dog dog = new Dog()
Dog.setName(“Hamish”)
You just can’t extend it
Can methods marked as final be overridden?
No
Can variables marked as final be reassigned?
No - constants
ie., MAX_VALUE
How to explain OOP to someone without a technical background
OOP stands for “Object-Oriented Programming,” and it’s a way for computers to understand how to do things.
Imagine you have a toy box with lots of different toys inside. Each toy is like an object, and each object has different things it can do.
For example, a toy car can move forward and backward, while a toy doll can move its arms and legs.
In OOP, we can tell the computer what each object can do and how it should behave when we interact with it.
So, it’s like teaching the computer to play with toys, but instead of just playing, the computer can use them to do all kinds of cool things!
Can you explain the basics of Javas memory model and how it affects multi threaded programming?
Java’s memory model states how memory actions (reads and writes) will appear to execute to the programmer
What is memory management?
Memory management is the process of removing unused objects to make space for new objects
Where do Java objects reside?
In the heap
When is the heap created?
The heap is created when the JVM starts up
What are the 3 main benefits of using generics?
reusable
type safe
flexible
What is a functional interface?
An interface that contains only a single abstract (unimplemented) method.
Describe 2 simalarities between abstract classes and interfaces
- Cant instantiate either of them
- Both can contain methods
Name 3 scenarios when an interface is a good choice?
- Multiple inheritance and different class hierarchies will be used
- Unrelated classes implementing the interface ie., Shape - rectangle, square
- A contract, but don’t care who impmements
Give an example of an interface?
interface Sender {
send ()
}
ImageSender implements Sender {
send() {
// image send code
}
Name 4 scenarios you would use an abstract class?
When you want too..
- Define a template for related classes to implement
- Enforce consistency across a class hierarchy
- Provide default behaviour to inherited methods
- Encapsulate implementation details
What is an example of an Abstract class
public abstract class vehicle {
public abstract void turnOn();
}
MotorCycle extends Vehicle {
public void turnOn(){ .. }
}
Car extends Vehicle {
public void turnOn(){..}
}
If we tried to use inheritance instead of an abstract class, Vehicle could be instantiated and this may mean we get behaviour we didn’t expect
What is the difference between an interface and an abstract class in Java?
An abstract class can have a state, but an interface can’t
For example, if we define
protected String colour;
in an abstract class it can only be accessed by subclasses of our abstract class
When should you use an interface instead of an abstract class
You should use an interface when you want multiple unrelated classes to have the same functionality
What does immutable mean?
An object’s state can’t be changed after it has been created
So it will behave the same way its whole lifetime
Is a String immutable?
Yes so if you checked if two Strings “Hello” were the same, they aren’t because they actually are objects with a different memory location
Are variables mutable by default?
Yes we can change the value
What are the benefits of immutable objects in Java?
- can share it safely between multiple threads
- are side effects free
What is the difference between polymorphism and inheritance?
Inheritance allows you to create new classes based on existing classes
Polymorphism allows objects to take on multiple forms through method overriding and overloading