Java Fundamentals
Tags: java, oop
Published: 2022-08-02 22:37:00

**What is an object?**
An instance of a class. 
Each object contains a state and behavior. 
State and behaviors are created through fields and methods. 

**What is a class?**
A class acts as a template/blueprint that describes the state and behavior of an object. 

---

**Constructors:** 

**How do you call a constructor?**
Use keyword _**new**_, this will instantiate a new object. 
Constructor is a method that makes your object. 

**What is default constructor?** 
Default constructor is a constructor that is not declared explicitly inside the class, or is defined but has no body or parameters that creates the objects for you. 

When you want to create an object with certain attributes then you want to create a constructor that takes in those attributes instead of having to set it every time. 

If you define another constructor (with arguments), default constructor will not be generated. If you still want one, you need to define it yourself.

--- 

**Keywords:**

**_this and super keywords:_**

Use the keyword **_this._** to set the fields to that instance of the object if the input and field have the same name. 

Use the keyword **_"super()"_** to call the parent's constructor and methods when using an inheritance relationship. 

Limitation on calling super - you can't call super() on a private method.

**_static keyword:_**

YouTube Resource: [Static in Java - How to use the Static Keyword](https://www.youtube.com/watch?v=yn9cDLVr_wk)

The static keyword means the value is the same for every instance of the class.
Without the static keyword, we need to create an instance of the class to access the method or variable within that class.
With the static keyword, we do not need to instantiate an object. However, the field and method need to also be static to be accessed. 


**_final_**

**What does final mean in method headings?**
The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override). 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxibirlkmos0jr4hjisr.png)

The combination of **_static final_** in Java is how to create a constant value.

---

**4 Pillars of OOP in Java:**
1. Abstraction 
2. Encapsulation 
3. Inheritance 
4. Polymorphism 

**Abstraction:**
Hide code implementation details from the user. We can do this by using abstract classes or interfaces. 

_**Interface vs Abstract class**_
YouTube Resource: [Abstract Classes and Methods - Learn Abstraction in Java](https://www.youtube.com/watch?v=52frlN8webg)

Abstract Class - Use keyword **_extends_** - Using inheritance concept. 

- An abstract class allows you to create functionality that subclasses can implement or override. 
- Cannot instantiate an abstract class aka not call the constructor of an abstract class because the purpose of is an abstract class is to create subclasses that is used and instantiated. 

Interfaces - Use keyword **_implements_** - Overrides methods from interface. 

- An interface only allows you to define functionality with unimplemented methods. Subclasses can then implement all functionalities from the interface. 

**Encapsulation:** 
Keep fields private, have public methods to access those private fields: Getters and setters. 
Reuse objects without allowing open access to the data system. 

**Inheritance:**
Parent class and child class. The child class can extent the parent class and inherit all the fields and methods that the parent contains. Great for code reusability. 

**Polymorphism:** 
Method overloading: Have different methods with the same name and different numbers of parameters. 

Method overriding: Same method name and number of parameters is overridden with new context. This can happen during inheritance where the child class overrides a parents class. 

Example: 
We have a sound() method in parent class that prints "papa bear voice". Child class that extends the parent class, but we want the sound() method to print "baby bear voice". We would override the method. 

---

**Access Modifiers:**

Where it can be accessed in your application.
Help restrict the visibility of classes, variables, methods, constructors, and etc.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s84ugexiy43emxpygbjd.png)

YouTube Resource: [Java Access Modifiers - Learn Public, Private, Protected and Default](https://www.youtube.com/watch?v=H0OetoieSDQ)

- Default: can be accessed within same package. 
```
int element = 30;
```
- Public: can be access anywhere, including different packages.
```
public int element = 30;
``` 
- Private: can only be accessed within the class. Class and interface cannot be private. 
```
private int element = 30;
```
- Protected: can only be accessed within same package, subclasses are also allowed.
```
protected int element = 30;
```
---

The **Object class** is the parent class of all the classes in java by default. In other words, it is the topmost class of java.

**What are some of the methods in Java Object class?** 

- toString() - returns the string representation of this object.
- hashCode() - returns the hashcode number for this object.
- equals(Object obj) - compares the given object to this object.

**What is the difference between == and .equals()?**
- Use == operators for reference comparison (address comparison)
- Use .equals() method for content comparison.

**Why don't you use == to compare strings in Java?**
- Using == will compare the reference whether it is the same object, not the value of the strings. So we need to use .equals()


**What is difference between HashMap and Map?**
Map is an interface. 
HashMap is a class. 
HashMap class implements the Map interface. 

Map has two implementations: 
HashMap and TreeMap 

---

**What is a JDK and JRE?**

- JDK(Java Development Kit) is used to develop Java applications. JDK also contains numerous development tools like compilers, debuggers, etc. 
- JRE(Java Runtime Environment) is the implementation of JVM(Java Virtual Machine) and it is specially designed to execute Java programs.