Today, instead of attending regular classes, we had a test to revise everything we learned since the beginning of our Java journey. Here's a detailed summary of all the key concepts we've discussed so far.
1.Class
A class is a blueprint or template used to create objects.
- It is declared using the class keyword.
- Inside a class, we can declare variables, methods, and constructors.
- Example:
class Main {
// variables, methods, constructors
}
-
2. Global and Local Variables
- Local Variables: Declared inside a method, only accessible within that method.
Global Variables: Declared outside methods but inside a class; accessible anywhere in the class. Also called fields.
Global variables are of two types:
- Static (class variables)
- Non-static (instance variables)
3. Data Types in Java
Java has two types of data types:
1.Primitive Data Types (fixed size):
byte, short, int, long, float, double, char, boolean
Default: int for integers, double for floating points.
2.Non-Primitive Data Types (reference types):
- Arrays, Strings, Classes...
4.Packages
Packages act like folders to organize classes.
There are two types:
- Built-in packages (like from Java API)
- User-defined packages
Example:
package myPackage;
Use javac -d . filename.java
to compile and java packagename.classname
to run.
5. Object
An object is an instance of a class that contains both state and behavior.
We create objects using the new keyword.
Example:
Main obj = new Main();
6. Constructor and Constructor Chaining
- A constructor has the same name as the class and no return type.
- Used to initialize objects with specific values.
- Java provides a default constructor automatically.
- Constructor Chaining is calling one constructor from another using this() as the first statement.
7.Inheritance
Inheritance allows one class to access the properties and methods of another class.
- Use extends keyword.
- Parent class: Super class
- Child class: Sub class
- Main benefit: Code reusability
8. Abstraction
Abstraction means showing only essential details and hiding unnecessary ones.
- Use abstract keyword for classes or methods.
- Cannot instantiate abstract classes directly.
- Abstract methods have no body — must be implemented by subclass.
9. Polymorphism
Polymorphism = One interface, many forms.
Two types:
1.Compile-time Polymorphism (Method Overloading)
- Same method name with different arguments or data types.
2.Runtime Polymorphism (Method Overriding)
- Same method in child and parent class.
10. Encapsulation
Encapsulation = Binding data and code together.
- Declare variables as private.
- Access and update them using getter and setter methods.
- Increases data security.
11. Interface
Another way to achieve abstraction.
- An interface is not a class; all methods are abstract by default.
- Use implements instead of
extends
. - Interfaces don’t have constructors.
12 & 13. this vs super / this() vs super()
this: Refers to current object
super: Refers to parent class object
this() is used to call another constructor in the same class.
super() is used to call the constructor of the parent class.
14. final Keyword
- final can be used with class, method and variables.
- Final class → cannot be inherited
- Final method → cannot be overridden
- Final variable → value cannot be changed
15. static vs non-static
- static: Belongs to class; shared across all objects
- non-static: Belongs to object; memory allocated at object creation
Static variables can be accessed using ClassName.variableName.
Non-static variables require an object reference.
Top comments (0)