Core Java - II:
Constructor:
A constructor is a block of code-like methods. It is called when an instance of a class is created. When the constructor is called, the memory for the object is allocated in heap memory.
2-types → No-arg constructor, Parameterized constructor.
Constructor overloading can also be done.
Reference:
- The physical address where a particular object is stored in memory is called a reference.
Static Variable:
A static variable acts as a common variable to all the instances of a class.
If the variable is changed in one instance of a class, the same will be reflected in all the other instances which are created before and after the change.
And a static variable can be directly accessed from a class ([i.e.], without being instantiated).
Instance Variable:
An instance variable is not common to all the instances of a class.
And an instance variable cannot be accessed directly from a class.
It should be accessed from the instance of the class ([i.e.], object of the class).
Instance variables are also called Member variables.
Static Method:
Static methods can be called directly from the class instead of an instance.
These methods are not dependent on the objects of a class and its variables.
Static methods cannot access instance methods and instance variables directly. It can only access static variables.
Instance Method:
Instance methods need to be called from an instance of a class.
Instance methods can access both instance and static methods and variables directly.
Method Overloading:
Multiple methods that have the same name but a different number of parameters and, the same or different return types.
The method to be run will be calculated according to the method name, return type and the number of arguments given during compilation.
Method Overloading is called Compile Time Polymorphism, as the compiler is the one that decides which method is to be run.
Both static and instance methods can be overloaded.
Method Overriding:
Defining a method in a child class that already exists in the parent class with the same name, the same number of parameters, and same return type.
Method overriding is called Run Time Polymorphism, as the method to be run is decided during runtime.
Only instance methods can be overridden.
Only inherited methods can be overridden.
Constructors, Private methods, and final methods cannot be overridden.
Autoboxing & Unboxing:
Autoboxing means converting a primitive int value to an Integer class value.
Unboxing means converting an Integer class value to a primitive int value.
Composition:
Composition describes a has-a relationship link.
Instead of extending a class and inheriting all the functionalities of a parent class, the class will be used as a datatype of an instance variable in another class. This way, the functionalities can also be inherited.
As Java doesn't support multiple inheritances, the same can be achieved by using composition.
We can create multiple instance variables with different datatypes, which in turn point to different classes in this case.
Generics:
These parameters are called Type Parameters.
Enables types (classes and interfaces) to be parameters when defining classes, interfaces, and methods.
The benefit is to eliminate the need to create multiple versions of methods or classes for various data types.
We can use one version of the methods to handle all the reference data types.
There is also bounded types where you can create the objects of a generic class to have data of specific derived types [e.g.] Number. In case the Number class is extended, only the subclasses of Number can be passed through that generic class as data.
Scope:
The part of the application where the variable is accessible is called the scope of that variable.
There are three types of scopes:
Class level → Accessed anywhere inside the class. The variables are called Class members.
Method level → Accessed only inside the method. The variables are called Local variables.
Block level → Accessed only inside blocks of code. [e.g.] Loops
Visibility/Access modifiers:
Visibility tells us how the class, methods and variables can be accessed.
There are five types of access modifiers:
Public → Members can be reached from anywhere.
Protected → Members can only be reached from the same class, or from within the class.
Internal → Members can only be reached from the same project.
Protected Internal → Members can only be reached from the same project and those classes that inherit from the class, even from another project.
Private → Members can only be reached by other members of the same class.
Final keyword:
If a variable is declared as final, the value of that variable cannot be changed.
If a method is declared as final, that method cannot be overridden.
If a class is declared as final, that class cannot be extended.
Packages:
The package is a mechanism to encapsulate a group of classes, sub-packages and interfaces.
It helps in preventing naming conflicts.
Packages can be considered as data encapsulation or data hiding.
Exception Handling:
Mechanism to handle runtime errors so that the normal flow of the application can be maintained.
Some exceptions are,
ClassNotFoundException
IOException
SQLException
RemoteException
Throwable class is the root class of the Java exception hierarchy inherited by two subclasses Exception and Error.
Types of Exception:
Checked Exception::
The classes that directly inherit the Throwable class except RuntimeException and Error are known as checked exceptions.
[e.g.] IOException, SQLException, etc.
Checked exceptions are checked at compile-time.
Unchecked Exception:
The classes that inherit the RuntimeException are known as unchecked exceptions.
[e.g.] ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.
Unchecked exceptions are checked at runtime.
Error:
Error is irrecoverable.
[e.g.] OutOfMemoryError, VirtualMachineError, AssertionError etc.
Keywords to handle exceptions:
try:
Specifies a block where the exception code should be placed.
Always followed by either catch or finally.
catch:
Used to handle the exception.
This can be followed by finally block later.
finally:
- Used to execute the code of the program whether an exception is handled or not.
throw:
- Used to throw an exception.
throws:
Used to declare exceptions.
Specifies that there may occur an exception.
It doesn’t throw an exception.
It is always used with method signature.