๐ง๐ต๐ฒ ๐๐ฎ๐๐ถ๐ฐ๐ ๐ข๐ณ ๐๐ฎ v๐ฎ ๐ข๐ข๐ฃ: ๐๐ผ๐ป๐๐๐ฟ๐๐ฐ t๐ผ๐ฟ๐ ๐ฎ๐ป๐ฑ ๐ ๐ฒ๐๐ต๐ผ๐ฑ ๐ข๐๐ฒ๐ฟ๐ฟ๐ถ๐ฑ๐ถ๐ป๐ด
You want to know what happens when you create an object in Java. Let's break down the core OOP concepts.
Here are the key points:
- How constructors work
- How inheritance behaves
- Why super() is important
- How method overriding works
- Why overriding is called Run Time Polymorphism
A constructor is a special block in Java that is automatically called when an object is created.
- Constructor name should be the same as the class name
- Constructors do not have a return type
- Constructors are automatically executed during object creation
- Constructors are mainly used to initialize object values
Inheritance is simple: when you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class.
The super keyword in Java is used to refer to the immediate parent class object.
- Used to call parent class constructors using super()
- Helps access parent class methods and variables when overridden or hidden
Method overriding means redefining a parent class method inside the child class with the same method name and same parameters.
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass
- Method overriding is used for runtime polymorphism
You can change return type in method overriding, but only if it is compatible.
Key takeaways:
- Constructors initialize objects
- Inheritance helps reuse code
- super() calls parent constructors
- Method overriding enables run time polymorphism
- Return type can be changed only if it is compatible
Source: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Optional learning community: https://dev.to/hariharan_sj_2003/behind-the-scenes-of-java-oop-constructors-to-run-time-polymorphism-m6m