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
What is method overriding?
Method overriding allows a subclass to provide its own implementation of a method that is already defined in its superclass
It must have the same name, return type, and parameter list as the method in the superclass
What is method overloading?
Method overloading is a feature that allows a class to have two or more methods with the same name, but with different parameters.
The Java compiler is able to tell the difference between the methods based on the number and/or types of parameters.
Describe inheritance?
Inheritance is a mechanism in object-oriented programming that allows a class to inherit properties and behaviors from a parent class.
In Java, a class uses “extends” keyword to inherit from another class
Allows you to create a new class that is a modified version of an existing class, without having to rewrite all of the code in the new class.
Describe polymorphism?
Polymorphism is the ability of an object to take on multiple forms. In Java, polymorphism is achieved through method overriding and method overloading.
What are 3 cache problems?
Cache penetration
Cache avalanche
Cache breakdown
What is a load balancer?
A load balancer is an algorithm that routes client requests (ie., database write, cache queries) across all servers
Like a traffic warden directing traffic
What is the Java Memory Model made up of?
Thread Stacks and a heap area
What is a thread stack?
An area of memory that contains variables, method calls etc
Can be fixed or variable size
If stack size exceeds the limit it will throw a Stack Overflow error
What is the heap?
Contains objects created during the application lifecycle
Will the heap change in size?
The heap size might change when the application runs
What happens when the heap is full?
Garbage is collected
What are key features of Java?
- Complied (converted to bytecode - run on platform with jvm)
- developed for class based and object oriented paradigms
- time effort to keep code readable
- strongly typed
- faster types assigned during compilation
Give an example of an interface
Ie., interface Shape
double area
Rectangle implements Shape
implementation of double area
What are 6 key concepts in Java?
Classes
Objects
Abstraction
Polymorphism
Inheritance
Encapsulation
What is a class?
a template for creating objects
has a variable, method, constructor
What are objects?
An object is an instance of a class that has a state and behaviours
What is abstraction
Abstraction allows us to hide the implementation from the user (in abstract classes / interfaces) and only show the user information needed to use the implementation
What is polymorphism?
Polymorphism means many forms. It lets us process data differently depending on the input.
What is inheritance?
Inheritance allows a new created class (child) to acquire the properties of an existing class (parent)
IS A relationship
What is encapsulation?
Encapsulation lets us hide the data and functions of a class . This helps protect it from being misused
Describe 4 benefits of using Inheritance?
Reduce the amount of duplicate code
Only need to change code in one place
Can override methods of the parent class
Keeps data private - derived class can’t alter it
Give 2 examples of what interfaces are useful for?
decoupling code
implementing polymorphism
Describe generics in Java
Generics allows you to write code that can work with a range of different data types in a type-safe manner
Classes, interfaces, and methods can operate on a parameterized type which is determined at compile time.
Generics use type parameters, which are enclosed in angle brackets (“<” and “>”).
Give an example of generics in Java
Why do we overwrite public boolean equals(Object o)
To define what it means to be equal
Describe 4 things an inherited class will have?
extends
super() constructor
override toString()
protected attributes
What are object arrays initally
null
What is a variable array initially?
0
What is instanceof used for?
instanceof is used to test if an object is of a given type
public class Round {}
public class Ring extends Round {}
@Test
void checkInstance(){
Ring ring = new Ring();
assertTrue(ring instanceof Round);
}
How does the instanceof operator work?
The instanceof operator works on the principle of the is-a relationship.
The concept of an is-a relationship is based on class inheritance or interface implementation.
Give an example of a violation of encapsulation?
public variables which allow anyone to change them directly
What happens if we try to override a final variable, method or class?
We’ll see a compiler error
… is final and cannot be overwritten
How can an abstract class in Java have a state?
An abstract class can have a state if it has variables and a constructor
Ie.,
public abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double getArea();
public abstract double getPerimeter();
}
public class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
}
What contributes to the state of an object in Java?
Both the constructor and the instance variables contribute to the state of a Java object. The constructor is responsible for initializing the instance variables to their initial state, while the instance variables themselves hold the values that define the object’s state at any given time.
What does state mean in OOP?
“state” typically refers to the set of values held in an object’s instance variables at any given time. An object’s state is defined by the values of its instance variables, and these values can change over time as the object interacts with the rest of the program.
What is memoization?