Hi everyone 👋 Continuing the weekend Java Keyword Series with another important keyword 👇 📌 Java Keyword Series – Part 2 ✅ super keyword in Java The super keyword is used to refer to the immediate parent class object 👇 🔹 Why do we use super? To access parent class variables To call parent class methods To call the parent class constructor 🔹 Where can we use super? 1️⃣ Access parent class variable class Parent { int x = 10; } class Child extends Parent { int x = 20; void show() { System.out.println(super.x); // prints 10 } } 2️⃣ Call parent class method class Parent { void display() { System.out.println("Parent method"); } } class Child extends Parent { void display() { super.display(); // calls parent method System.out.println("Child method"); } } 3️⃣ Call parent class constructor class Parent { Parent() { System.out.println("Parent constructor"); } } class Child extends Parent { Child() { super(); // calls parent constructor } } 🔹 In simple words super is used to access or call members of the parent class. 👉 🧠 Quick Understanding super.variable → parent variable super.method() → parent method super() → parent constructor #Java #CoreJava #JavaKeywords #LearningInPublic #BackendDevelopment
Java Super Keyword: Accessing Parent Class Variables and Methods
More Relevant Posts
-
Hi everyone 👋 Continuing the weekend Java Keyword Series with another important keyword 👇 📌 Java Keyword Series – Part 3 Today let’s understand one of the most important multithreading keywords in Java 👇 🔐 synchronized Keyword in Java The synchronized keyword is used to control thread access to shared resources. It ensures: - Mutual Exclusion (Only one thread at a time) - Visibility of changes - Thread Safety 🔹 Why Do We Need synchronized? In multithreading, multiple threads may try to access or modify the same object. Example problem: class Counter { int count = 0; public void increment() { count++; } } If two threads call increment() simultaneously, the result may be incorrect. Because count++ is NOT atomic. 🔹 Solution Using synchronized class Counter { int count = 0; public synchronized void increment() { count++; } } Now: - Only one thread can execute increment() at a time - Other threads must wait 🔹 How Does It Work Internally? Every object in Java has a monitor lock. When a thread enters a synchronized method/block: - It acquires the object’s lock - Other threads must wait - Lock is released when method finishes 🔹 In Simple Words synchronized ensures that only one thread at a time can access a critical section of code. #Java #Multithreading #Synchronized #CoreJava #InterviewPreparation #BackendDeveloper
To view or add a comment, sign in
-
🚀 Java 8 Complete Feature List – Day 1 of My Java 8 Series Java 8 (released in 2014) was one of the biggest upgrades in Java history. It didn’t just add features. It changed the way we write Java. With Java 8, Java officially stepped into functional programming, cleaner code, and powerful data processing. Here’s a complete list of important Java 8 features 👇 1️⃣ Lambda Expressions → Write shorter, cleaner code → Replace anonymous inner classes 2️⃣ Functional Interfaces → Single abstract method → Predicate → Function → Consumer → Supplier → UnaryOperator → BinaryOperator 3️⃣ Method References (::) → Static method reference → Instance method reference → Constructor reference 4️⃣ Stream API 🔥 → filter() → map() → reduce() → collect() → Parallel Streams 5️⃣ Default Methods in Interfaces → Method implementation inside interfaces → Backward compatibility 6️⃣ Static Methods in Interfaces 7️⃣ Optional Class → Better null handling → Avoid NullPointerException 8️⃣ New Date & Time API (java.time) → LocalDate → LocalTime → LocalDateTime → ZonedDateTime → Period & Duration 9️⃣ CompletableFuture → Asynchronous programming → Non-blocking operations 🔟 Nashorn JavaScript Engine 1️⃣1️⃣ Base64 Encoding & Decoding 1️⃣2️⃣ Repeatable & Type Annotations 1️⃣3️⃣ Collection API Enhancements → forEach() → removeIf() → replaceAll() → computeIfAbsent() → merge() 1️⃣4️⃣ Arrays.parallelSort() 1️⃣5️⃣ Concurrency Enhancements → Fork/Join improvements → StampedLock 📌 Over the next few days, I’ll break down each feature with: Real-world examples Interview-focused explanations Practical use cases If you're preparing for interviews or want to write cleaner Java code, this series will help. Follow along 🚀 #Java #Java8 #BackendDevelopment #SoftwareEngineering #Coding #InterviewPreparation #SpringBoot
To view or add a comment, sign in
-
Text Block simple example In this post under Java Core, I will introduce you to newly added "Text Block" feature. This feature was added as part of Java 15. Pre Java 15, if we have to declare a multiline string we used to declare it as shown below String result = "Since Java 15, text blocks are \n" + "available as a standard feature.\n" + "With Java 13 and 14, \n" + "we needed to enable it as a preview feature.";...
To view or add a comment, sign in
-
🔥 String vs StringBuilder in Java In Java, String is immutable and StringBuilder is mutable — and that makes a big difference in performance. 🔹 String • Immutable (cannot be changed after creation) • Every modification creates a new object in memory • Slower when used inside loops • Thread-safe ⚠️ Repeated concatenation (like in loops) leads to unnecessary object creation and memory usage. 🔹 StringBuilder • Mutable (modifies the same object) • No new object created for each change • Faster and memory efficient • Not thread-safe 🚀 Best choice for frequent string modifications, especially inside loops. 🎯 When to Use? ✅ Use String → When value doesn’t change ✅ Use StringBuilder → When performing multiple concatenations 💡 In backend applications, choosing StringBuilder for heavy string operations improves performance significantly. #Java #BackendDevelopment #JavaProgramming #Performance
To view or add a comment, sign in
-
-
📌 Switch Expressions in Java – Finally, No More Fall-Through Bugs 🚀 If you're preparing for modern Java interviews, you must know this feature. 👉 Introduced as preview in Java 12 👉 Became stable in Java 14 And it fixed one of the most annoying things in Java. 🤯 The Old Switch Problem switch(day) { case MONDAY: return 1; case TUESDAY: return 2; default: return 0; } Issues: - Verbose - Easy to forget break - Fall-through bugs - Not expressive 🚀 Switch Expression (Modern Way) int result = switch(day) { case MONDAY -> 1; case TUESDAY -> 2; default -> 0; }; Cleaner. Safer. More readable. No break. No accidental fall-through. 🔥 Multi-Line Case Example int result = switch(day) { case MONDAY, TUESDAY -> 1; case WEDNESDAY -> { System.out.println("Midweek"); yield 2; } default -> 0; }; Yes — yield returns a value from a block. 🔑 Final Thought Switch Expressions didn’t just improve syntax. They made switch: - Safer - More functional - More predictable Modern Java is about reducing accidental complexity. #Java #ModernJava #Java14 #SoftwareEngineering #InterviewPreparation
To view or add a comment, sign in
-
-
🚀 Java 8 Series – Day 2 Understanding Lambda Expressions Before Java 8, writing simple logic required a lot of boilerplate code. Example: Creating a Comparator Before Java 8: Comparator comp = new Comparator() { @Override public int compare(String s1, String s2) { return s1.length() - s2.length(); } }; Too much code for a small logic, right? Now with Java 8 Lambda: Comparator comp = (s1, s2) -> s1.length() - s2.length(); One line. Same logic. Much cleaner. What is a Lambda Expression? A lambda expression is an anonymous function that: • Has no name • Has no modifier • Has no return type declaration • Can be passed as an argument It is mainly used to implement Functional Interfaces. Basic Syntax: (parameters) -> expression OR (parameters) -> { block of code } Examples: 1️⃣ No parameter () -> System.out.println("Hello") 2️⃣ Single parameter x -> x * x 3️⃣ Multiple parameters (a, b) -> a + b Why Lambda Was Introduced? • Reduce boilerplate code • Enable functional programming • Improve readability • Make collections processing powerful (Stream API) Where Are Lambdas Commonly Used? • Comparator • Runnable • Event handling • Stream API operations (filter, map, reduce) Interview Question: 1. Why can lambda be used only with Functional Interfaces? (Answer coming in Day 3 😉) In the next post, I’ll explain Functional Interfaces in depth with real interview examples. Follow the series if you're preparing for Java interviews 🚀 #Java #Java8 #Lambda #BackendDevelopment #Coding #SoftwareEngineering
To view or add a comment, sign in
-
📌Exception Handling in Java ⚠️ ✅Exception Handling is a mechanism to handle unexpected situations that occur while a program is running. When an exception occurs, it disrupts the normal flow of the program. Common examples: • Accessing an invalid index in an array→ ArrayIndexOutOfBoundsException • Dividing a number by zero→ ArithmeticException Java provides thousands of exception classes to handle different runtime problems. 📌 Types of Exceptions in Java 1️⃣ Built-in Exceptions These are predefined exceptions provided by Java. ✅Checked Exceptions -Checked by the compiler at compile time Must be handled using try-catch or declared using throws Examples: IOException SQLException ClassNotFoundException ✅Unchecked Exceptions -Not checked by the compiler at compile time Occur mainly due to programming errors Examples: ArithmeticException NullPointerException ClassCastException 2️⃣ User-Defined (Custom) Exceptions Java also allows developers to create their own exceptions. This is useful when we want to represent specific business logic errors. Basic rules to create a custom exception: 1️⃣ Extend the Exception class 2️⃣ Create a constructor with a message 3️⃣ Throw the exception using throw 4️⃣ Handle it using try-catch 📌 Finally Block ✅The finally block always executes after the try-catch block, whether an exception occurs or not. It is commonly used for cleanup tasks, such as: Closing database connections Closing files Releasing resources 📌 Try-With-Resources ✅Sometimes developers forget to close resources manually. To solve this problem, Java introduced Try-With-Resources. It automatically closes resources once the block finishes execution. This makes resource management safer and cleaner. 📌 Important Keywords ✅throw : Used to explicitly create and throw an exception object. ✅throws: Used in the method signature to indicate that a method may throw an exception. Grateful to my mentor Suresh Bishnoi Sir for explaining Java concepts with such clarity and practical depth . If this post added value, feel free to connect and share it with someone learning Java. #Java #ExceptionHandling #CoreJava #JavaDeveloper #BackendDevelopment #SoftwareEngineering #InterviewPreparation #JavaProgramming #CleanCode
To view or add a comment, sign in
-
-
🚀 Java 8 Series – Day 3 Understanding Functional Interfaces In Day 2, we discussed Lambda Expressions. But here’s the important rule: A Lambda Expression can only be used with a Functional Interface. So today, let’s understand what that actually means. What is a Functional Interface? A Functional Interface is an interface that contains: Exactly ONE abstract method. That’s it. Example: @FunctionalInterface interface Calculator { int operate(int a, int b); } Now we can implement it using Lambda: Calculator add = (a, b) -> a + b; Why Only One Abstract Method? Because Lambda expressions provide the implementation of that single method. If there were multiple abstract methods, Java wouldn’t know which one the lambda is implementing. What is @FunctionalInterface? It is an optional annotation. If you accidentally add a second abstract method, the compiler will throw an error. It helps enforce the rule. Built-in Functional Interfaces in Java 8 Java 8 introduced many ready-made functional interfaces in the java.util.function package. Most commonly used ones: 1️⃣ Predicate Takes input, returns boolean Example: x -> x > 10 2️⃣ Function<T, R> Takes input, returns output Example: x -> x * 2 3️⃣ Consumer Takes input, returns nothing Example: x -> System.out.println(x) 4️⃣ Supplier Takes no input, returns output Example: () -> new Date() 5️⃣ UnaryOperator Takes one input, returns same type 6️⃣ BinaryOperator Takes two inputs, returns same type Real Interview Question: What is the difference between Predicate and Function? (Answer: Predicate returns boolean. Function returns any type.) Why Functional Interfaces Matter? They are the foundation of: • Lambda Expressions • Stream API • Method References • Functional programming in Java Without understanding Functional Interfaces, Java 8 will never feel complete. Tomorrow: Method References (::) Cleaner than Lambdas in many cases 👀 Follow the series if you're serious about mastering Java 8 🚀 #Java #Java8 #FunctionalInterface #BackendDeveloper #Coding #InterviewPreparation
To view or add a comment, sign in
-
Java Unchecked Exceptions – The Surprise You Didn’t See Coming! Ever been on a roller coaster ride? 🎡 👉 You expect thrills and twists 😃 👉 But suddenly, your phone slips out of your pocket 📱💥 👉 Or you scream so loud you lose your voice 😂 These are unexpected problems that you didn’t plan for in advance. That’s what Unchecked Exceptions are in Java! 🔹 They occur at runtime, not compile-time 🔹 Compiler won’t force you to handle them 🚦 🔹 If not handled, they crash the program ❌ 💻 Example Code public class UncheckedExample { public static void main(String[] args) { try { int numbers[] = {1, 2, 3}; System.out.println(numbers[5]); // Accessing invalid index } catch (ArrayIndexOutOfBoundsException e) { System.out.println("⚠️ Oops! You tried to access something outside the ride limits: " + e.getMessage()); } } } ✅ Output: ⚠️ Oops! You tried to access something outside the ride limits: 5 ✨ Takeaway: Unchecked Exceptions are like surprises during the ride 🎢. Java lets your code compile ✅, but if you don’t handle them, they may cause a crash at runtime 💥.
To view or add a comment, sign in
-
-
Day 11 - What I Learned In a Day(JAVA) Today I learned that Java variables are classified into two areas and understood their scope. 1️⃣ Global Area (Instance Variable) Declared inside the class but outside methods. Accessible by all methods inside the class. Scope: Entire class. Memory is created when the object is created. 2️⃣ Local Area (Local Variable) Declared inside a method, constructor, or block. Accessible only inside that method or block. Scope: Limited to that block only. Memory is created when the method runs. Types of Variables in Java (Based on Scope): 1️⃣ Local Variable Declared inside a method. Used only inside that method. Cannot be used outside the method. 2️⃣ Non-Static Variable (Instance Variable) Declared inside the class but outside methods. Belongs to the object. Each object has its own copy. 3️⃣Static Variable Declared using static keyword. Belongs to the class. One copy is shared by all objects. Three Important Statements in Java (Variables): 1️⃣ Declaration Creating a variable. You are telling Java the type and name of the variable. No value is given. Example: int a; 2️⃣ Initialization Giving a value to a variable. The variable must already be declared. Example: a = 10; 3️⃣ Declaration + Initialization Creating the variable and giving value at the same time. Example: int a = 10; Practiced 👇 #Java #Variables #Programming #CodingJourney
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development