Posts

Showing posts with the label java oops

Java final Keyword (final variable, final method, final class)

Image
Java final Keyword (final variable, final method, final class) - With Examples Quick Summary Further, in this post, you will get to know about the concept and uses of the final keyword along with its subparts, i.e., final variable, final method, and final class, and how it is helpful to make Java code more secure. Java Final Keyword OOP Interview In Java, certain things are best left unchanged once they are created. For instance, a constant data item such as PI, or a method that is not intended to be overridden. That’s when the final keyword is extremely handy. 1) What is final Keyword in Java? The final keyword in Java is used to restrict something. It can be applied to: Variable → value cannot be changed Method → method cannot be overridden Class → class cannot be inherited ✅ Simple meaning: final = "don’t change this" 2) final Variable in Java A final v...

Java super Keyword (super variable, super method, super constructor)

Image
Java super Keyword: super Variable, super Method, super Constructor (With Examples) ✨ Quick Summary In this post, you’ll learn what the super keyword means in Java and why it is used. We will cover super variable , super method , and super constructor with beginner-friendly examples. Java super keyword Inheritance In our previous posts, we learned about Inheritance , Polymorphism , and the this keyword. Now, let’s learn another keyword that is used mainly in inheritance: super . 1) What is super Keyword in Java? The super keyword in Java refers to the parent class object . ✅ Simple meaning: super = parent class reference 2) Why Do We Use super Keyword? We use super keyword mainly for: ✅ To access parent class variables ✅ To call parent class methods ✅ To call parent class constructor 3) super Keyw...

Java this Keyword

Image
Java this Keyword: this Variable, this Method, this Constructor (With Examples) ✨ Quick Summary In this post, you’ll learn what the this keyword means in Java and why it is used. We will cover this variable , this method , and this constructor with simple examples. Java this keyword OOP In our previous post, we learned about the static keyword in Java. Now, let’s learn another very important keyword used in constructors and OOP: this . 1) What is this Keyword in Java? The this keyword in Java refers to the current object . ✅ Simple meaning: this = the current class object 2) Why Do We Use this Keyword? We use this keyword mainly for: ✅ To differentiate between instance variables and parameters ✅ To call current class methods ✅ To call current class constructor ( constructor chaining ) 3) this Keyword to...

Packages in Java

Packages in Java: Beginner Guide (With Examples) ✨ Quick Summary In this post, you’ll learn what Packages are in Java and why they are used. We will cover built-in packages , user-defined packages , import statement, and simple examples to help you understand clearly. Java Packages Import In our previous post, we learned about Interfaces in Java. Now, let’s learn a very important topic used in every Java project: Packages . 1) What is a Package in Java? A package in Java is used to group related classes and interfaces. Think of a package like a folder on your computer. Inside that folder, you store similar files together. ✅ Example: java.util is a package that contains classes like ArrayList , Scanner , Date , etc. 2) Why Do We Use Packages? Packages ...

Interface in Java

Interface in Java: Complete Beginner Guide (With Examples) ✨ Quick Summary In this post, you’ll learn what an Interface is in Java and why it is used in OOP. We will cover interface syntax , implements keyword , multiple inheritance , and real beginner-friendly examples. Java OOP Interface Multiple Inheritance In our previous post, we learned about Abstraction and how Java hides implementation details. Now, let’s learn another very important concept: Interface in Java . 1) What is an Interface? An interface in Java is a blueprint of a class. It contains method declarations that must be implemented by the class. In simple words: ✅ Interface tells what to do ❌ Interface does not tell how to do (implementation is in class) 2) Why Do We Use Interface? Interfaces are used for: ✅ Achieving abstraction ✅ Achieving multiple inheritance ✅ Creating a standard structure ✅ Writing flexible and reusa...

Polymorphism in Java (Method Overloading + Method Overriding)

Polymorphism in Java: Overloading and Overriding (With Examples) ✨ Quick Summary In this post, you’ll learn what Polymorphism is in Java and how it works in OOP . We will cover Method Overloading (Compile-time Polymorphism) and Method Overriding (Run-time Polymorphism) with simple examples. Java OOP Polymorphism Overloading / Overriding In our previous post, we learned about Inheritance and how one class can acquire properties of another class. Now let’s learn another powerful OOP concept: Polymorphism . 1) What is Polymorphism? The word Polymorphism means: many forms . In Java, polymorphism means: ✅ One method can behave differently in different situations ✅ One object can take multiple forms 2) Types of Polymorphism in Java Java has 2 main types of polymorphism: Compile-time Polymorphism (Method Overloading) Run-time Polymorphism (Method Overriding) 3) Method Overloading (Compile-ti...

Java Constructors (Default + Parameterized)

Java Constructors: Default and Parameterized (With Examples) ✨ Quick Summary In this post, you’ll learn what Constructors are in Java and why they are used. We will cover default constructors , parameterized constructors , and how constructors help in initializing objects with simple examples. Java OOP Constructor OOP Concepts In our previous post, we learned about Java OOP and understood the basics of Class and Object . Now it’s time to learn an important OOP concept: Constructors . 1) What is a Constructor? A constructor is a special method used to initialize objects in Java. It is called automatically when an object is created. Example: Student s1 = new Student(); When this line runs, the constructor gets called automatically. 2) Constructor Rules in Java ✅ Constructor name must be same as the class name ✅ Constructor does not have a return type (not even void) ✅ Constructor is called aut...