Java Day 32: Static Concepts & Inheritance

🚀 Day 32 – Core Java | Static Concepts & Introduction to Inheritance Today’s session connected two important concepts in Java: Static members and the second pillar of OOP – Inheritance. 🔑 Key Concepts Learned ✔ Static Variable Belongs to the class, not the object Only one copy exists in memory Used when a value is common for all objects Example: Bank interest rate or mathematical constant π ➡ Advantage: Efficient memory utilization ✔ Static Block Static blocks execute during class loading, even before the main() method. Two major uses: • Initialize static variables • Execute code before the program enters main() Execution order: Static Variables → Static Block → main() Method Multiple static blocks are allowed, and they execute in the order written in the program. ✔ Static Method Static methods can be accessed without creating an object. Example scenario: Converting Miles → Kilometers does not depend on any object. So the method can be declared as static. Called using: ClassName.methodName() Example: Car.milesToKilometer(380); ➡ Advantage: No object creation → Better memory usage ✔ Static vs Instance Access Rule Important concept from Java execution: • Static members can access only static data directly • Instance members can access both static and instance data Reason: Instance variables get memory only when an object is created, but static members execute during class loading. So static code cannot access something that doesn't yet exist in memory. 🧩 Introduction to Inheritance (2nd Pillar of OOP) Inheritance allows one class to acquire properties and behaviors of another class. Definition: Inheritance is the process where one class acquires the properties and behaviors of another class. Example: class Hacker extends BankAccount Here: • BankAccount → Parent / Superclass • Hacker → Child / Subclass ✔ Advantages of Inheritance • Code Reusability • Reduced development time • Less effort in building similar applications Real-world example: Many games reuse mechanics from earlier games, modifying only certain features instead of rebuilding everything. ✔ Important Rule Private members do NOT participate in inheritance Reason: Private data is protected by Encapsulation, and inheritance cannot violate that protection. ✔ First Type of Inheritance Single Inheritance Parent Class → Child Class Example: BankAccount → Hacker 💡 Biggest Takeaway Understanding static execution, memory behavior, and inheritance helps developers write code that is: Efficient Reusable Scalable These concepts form the foundation for advanced Java topics like polymorphism and abstraction. #Day32 #CoreJava #JavaOOP #StaticKeyword #Inheritance #JavaProgramming #DeveloperJourney #LearningJava

To view or add a comment, sign in

Explore content categories