DEV Community

Tammy Vo
Tammy Vo

Posted on • Updated on

Java Fundamentals

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

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

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

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

YouTube Resource: Java Access Modifiers - Learn Public, Private, Protected and Default

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

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.

Top comments (0)