Inheritance
Inheritance
Uses the Extends keyword
Java uses Single Inheritance
Only one class can be inherited at a time
ex) Dog extends Animal
=> The Dog class inherits from the Animal Class
The parent class can be accessed with the super keyword
=> super();
The implicitly called super() invokes the parent's default constructor
The parent constructor of all objects is Object!!!!!!!!
When loaded into memory, the parent is on top and the child is on the bottom
When searching for data, it goes from the bottom child upward to the parent
=> It searches from the bottom and goes up if not found
ex) If the data type is Dog, both Animal and Object are accessible
Using super, you can skip one child class and search from the parent
is-a relationship
=> The data type of all objects can be the parent
Data compatibility is only possible in parent-child relationships
Not possible in sibling relationships!
A parent type cannot access child data at the bottom
super();
super();Calls the parent's constructor.
Even if removed, it is automatically included!
Even if absent, it is automatically called!
If not visible, it is omitted!
Like the this() method, it is only allowed in the first statement!
So you can tell that super() is absent in constructors where the this() method is used
super.______ allows searching from the parent area instead of the child!
Both
this. keywordandsuper. keywordcan only be used in the heap area!!!!
Method Overloading
Multiple methods with the same name can exist within a single class
However, the type or number of the method's parameters must be different!
Constructor
A function created when an object is created
The constructor function name is the same as the class name
Return type must not be mentioned - method overloading applies
Constructors can also be called by method name
this();
A constructor with no parameters = default constructor
public can be added or omitted
Access modifiers can be specified
Overriding in Java
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Redefining a function inherited from the parent!
Just bring over the method declaration as is!
Overriding is not mandatory!
So no error is shown.. fix it yourself~
If inheritance is not mentioned, it can be seen that java.lang.Object is automatically inherited!
Class Animal extends Object!
Object= methods available to all objectsAt the very top of any object is Object!
Is a
The data type of all objects can be the parent!!
The data type of all objects is Object!
why? Because Object is at the very top of all objects' memory!
ex1)
Since the parent object of Animal is Object, Object can also be used as the data type when declaring an object!
However, if the type is Object, the data inside Animal cannot be accessed, and the accessible type is limited to the very top!
ex2)
Since Dog has Animal as a parent object, Animal or Object can be used when declaring an object!
When declared as Animal, Dog is not accessible; Animal and above (Animal + Object) are accessible
All objects in Java can be of Object type, but the accessible information is limited!
The parent object can be accessed through type casting!
=> Casting in objects -> Up casting
Ex) ((Animal)d).kind;
β => If you up-cast Dog, which is originally Dog, to Animal, you can directly access Animal
Annotations
: a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.
-> ex) @Override
Polymorphism
the ability of an object to take on many forms
By incorporating overriding techniques, a single method can accept various types of objects!
Last updated