In Java, a class is not final, but we need to restrict the creation of a child class. How is it possible? By declaring each constructor as private, we can stop the child classes. eg: # parent class class P{ private P(){} } # child class class C extends P{ public C(){ super(); // ---> tries to call parent's constructor } } super() will attempt to call the parent no-arg constructor, but since it is private, the compiler throws an error. So even though the class is not final, inheritance becomes impossible. Would love to know if there are other ways to restrict child class creation! #java #oops #constructer
Raushan Kumar’s Post
More Relevant Posts
-
Hello everyone! To start this with I will share this article from a few months ago by Anthony Goubard on the Friends of OpenJDK community blog. It’s a series about some lesser-known (or not-at-all known) features of the Java language, in this particular case, it deals with file and line readers, those old time classes we all used when learning the language but maybe forgotten by the framework frenzy of latter years. #Java #OpenJDK #Software
To view or add a comment, sign in
-
Day 3/50 | #50DaysOfCode 📍 Platform: LeetCode 💻 Language: Java ✅ 2520. Count the Digits That Divide a Number (Easy) Today’s problem focused on digit extraction and divisibility checks. It helped strengthen my understanding of number manipulation and conditional logic. 🔎 Approach: Extract each digit using modulus (%) Check if the digit divides the original number using modulo If divisible, increment the counter Continue until all digits are processed Return the final count 📌 Example: Input: num = 1248 Output: 4 Explanation: All digits (1, 2, 4, 8) divide 1248 evenly This problem improved my understanding of loops, digit handling, and divisibility logic in Java. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #Consistency #LearningJourney #50DaysOfCode #LinkedIn
To view or add a comment, sign in
-
-
Leetcode Problem || Complement of Base 10 Integer(1009) 🚀 The Bitwise Complement problem using Java. 🔹 Idea: The complement of a number means flipping all bits in its binary representation (0 → 1 and 1 → 0). Instead of manually converting the number to binary, I used a bit manipulation trick: 1️⃣ Find the highest set bit using Integer.highestOneBit(n) 2️⃣ Create a mask with all bits set to 1 up to that position 3️⃣ Use XOR (^) with the mask to flip the bits #LeetCode #Java #BitManipulation #CodingPractice #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 21 of #100DaysOfLeetCode 💻✅ Solved #27. Remove Element problem on LeetCode in Java. Approach: • Used a two-pointer technique to modify the array in-place • Maintained a pointer k to place elements not equal to val • Iterated through the array and skipped elements equal to val • Placed non-val elements at the k-th position and incremented k • Returned k as the number of remaining elements Performance: ✓ Runtime: 0 ms (Beats 100% submissions) ✓ Memory: 44 MB (Beats ~75% submissions) Key Learning: ✓ Strengthened understanding of in-place array manipulation ✓ Learned how to skip unwanted elements efficiently ✓ Improved confidence in two-pointer techniques for array problems Learning one problem every single day 🚀 #Java #LeetCode #DSA #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 22 – Core Java Journey Today I learned about: 🔹 Encapsulation Encapsulation is one of the fundamental concepts of Object-Oriented Programming in Java. It means wrapping data (variables) and code (methods) together into a single unit (class). 👉 We achieve encapsulation by: Declaring variables as private Providing public getter and setter methods 🎯 Benefits: Data hiding Better security Controlled access Improved maintainability 🔹 Constructors A constructor is a special method used to initialize objects. ✔ Constructor name must be the same as the class name ✔ It does not have a return type ✔ Automatically called when an object is created Types of constructors: Default Constructor Parameterized Constructor #Day22 #CoreJava #JavaProgramming #OOPS #Encapsulation #Constructors #LearningJourney #WomenInTech
To view or add a comment, sign in
-
-
I used to think equals() and == in Java were basically the same. They’re not. == checks if two references point to the same memory location. equals() checks if two objects are logically equal. That difference looks small. Until your HashMap stops working properly. In Java, if you override equals(), you must also override hashCode(). Because collections like HashMap use hashCode() first to find the bucket, and then equals() to confirm the match. Forget one of them… and your object becomes “invisible” inside the map. One small contract. One big lesson. #Java #BackendDevelopment #SoftwareEngineering #Programming
To view or add a comment, sign in
-
Day 3/50 | #50DaysOfCode 📍 Platform: LeetCode 💻 Language: Java ✅ 977. Squares of a Sorted Array (Easy) Today’s problem focused on array manipulation and sorting logic. It helped me understand how negative numbers affect order after squaring and how to maintain sorted order efficiently. 🔎 Approach: Traverse the array and calculate the square of each element Store the squared values in a new array Sort the new array in non-decreasing order Return the sorted squared array 📌 Example: Input: nums = [-4, -1, 0, 3, 10] Output: [0, 1, 9, 16, 100] This problem strengthened my understanding of arrays, sorting, and handling negative values in Java. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #Consistency #LearningJourney #50DaysOfCode #LinkedIn
To view or add a comment, sign in
-
-
Day 68 of #100DaysOfCode 🚀 Today I implemented Pascal’s Triangle using Java. At first glance, it looks like just a pattern problem. But internally, it teaches something powerful — building current state from previous state. 🔹 The first and last elements of every row are always 1 🔹 Every middle element = sum of two elements from the previous row 🔹 This is a classic example of bottom-up thinking Instead of hardcoding values, we dynamically generate each row based on the previous one. This problem strengthened: Nested loop understanding 2D List handling in Java Pattern recognition Thinking in terms of recurrence Small problems. Strong foundations. Consistent progress. On to the next one 💪 #100DaysOfCode #Java #DSA #ProblemSolving #LearningInPublic #dsawithkunal
To view or add a comment, sign in
-
-
💡 Ever wondered what actually happens when you run javac in Java? Most developers write Java programs every day, but many don’t fully understand what happens behind the scenes when the Java compiler processes our code. To help beginners and aspiring developers understand this clearly, I created a video where I explain how the Java compiler works step by step in a very simple way. In this video, I cover: 🔹 Lexical Analysis – How the compiler breaks code into tokens 🔹 Syntax Analysis – How Java checks the structure of the program 🔹 Semantic Analysis – How the compiler verifies the meaning and correctness of the code 🔹 How the .java file is finally converted into bytecode (.class file) My goal was to explain these concepts in the simplest possible way so that beginners can easily understand what happens inside the Java compiler. 🎥 Watch the full video here: https://lnkd.in/geqpDTAp If you are learning Core Java, Computer Science fundamentals, or preparing for interviews, understanding this process will give you a much stronger foundation. I’d love to hear your feedback after watching! #Java #CoreJava #JavaCompiler #Programming #SoftwareEngineering #LearnJava #ComputerScience #Coding
How Java Compiler Works? | Lexical, Syntax & Semantic Analysis
https://www.youtube.com/
To view or add a comment, sign in
-
Day 30/100 - LEETCODE Challenge ✅ Problem : Ransom Note Solved the Ransom Note problem using Java! I implemented an efficient approach using a frequency array to count the occurrences of each character in the magazine string and then reduced the count while checking the ransomNote string. If any character count becomes negative, it means the magazine does not have enough letters to construct the ransom note. This solution runs in O(n + m) time complexity with constant space (26 letters), making it highly optimized. ✅ Runtime: 1 ms (Beats 99.85%) ✅ Memory: 46.31 MB Great practice for understanding character frequency, arrays, and greedy checking techniques in string problems. #100DaysOfCode #java #Coding #SoftwareDeveloper
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