6.16 OOP This and Super

Hello welcome to this new course dedicated to the keywords this and supe The super keyword is used to access/call ... the members of the parent class that is to say the variables and methods. The keyword this is used to call members ... of the current class (variables and methods). This is required when we have a parameter with the same ... name as an instance variable (field). We can use them anywhere in a class, except in static areas (the ... static block or a static method). Any attempt to do so will lead to compilation errors in the following ... course we will have more information about the method or static block The keyword this is commonly used with builders and setters, and possibly in getters. In the example below, ... we use the keyword this in the constructor and the setter since there is a parameter with the same name. In the getter, we don’t have any parameters, so this keyword is optional. The super keyword is commonly used in cases where we want to call a method, when we call a ... method with the same name in the parent class. In the example below, we have a printMethod method that calls super.printMethod.In other words ... it calls the method of the same name of the parent class. Without the super keyword in this case, the call ... would be recursive. This means that the method would call itself forever (or until memory is fully used). That’s why the super keyword is needed. In Java, we have the call this() and the call super() it looks like an ordinary method call. Use this() to ... call a builder from another overloaded builder of the same class. The call to this() can only be used in ... a manufacturer, and it must be the manufacturer’s first declaration. It is used with the chaining of builders, ... that is to say when a builder calls another builder. and reduces duplicate code. The only way to call a parent constructor ... is to call super(). this call, call the parent class builder. The Java compiler sets a default call to super () if it is not added, and it is always the ... super that is inserted by the compiler (constructor without arguments). The call to super () must be the first statement in each ... constructor.Even abstract classes have constructors, although you can never instantiate an abstract class using the keyword ... new. An abstract class is always a super class, so its constructors run when someone creates an instance of a ... concrete subclass. A builder can have a call to super () or this() but never both together. In this example, we have three builders. All three constructors initialize Variables. There is repeated code ... in each constructor. We initialize the variables in each constructor with some default values. You should never write ... a builder that way. let’s look at the right way to use a call this(). we have three Builders. The first constructor calls second constructor and the second constructor calls the third constructor and the ... 3rd constructor initializes the instance variables. The 3rd constructor does all the work No matter which constructor we call, ... the variables will always be initialized in the 3rd constructor This is called the chain of constructors, the last constructor ... has the "responsibility" to initialize the variables. We’ll compare the two examples on the screen now. So on the left side, again...this is the bad example of builders on the right side we have a good example. So the problem with ... the left side is the duplicate code. So the three constructors on the left side initialize the variables. Compare to ... the solution on the right side. Here we have a constructor who initializes the variables, and the other constructors just call each other, so we end up in the third constructor. And… it’s still the chain of builders. So with this chain of builders. We can undo, avoid duplication of code, ... and besides the code on the left side can lead to many bugs and much more work from you ... and it’s also bad practice. In this example, we have a Carre class with x,y variables and a Rectangle class that extends Carre with ... variables height and width In Rectangle, the first constructor, We call the second constructor The second constructor calls ... the parent constructor with the x and y parameters The parent constructor will initialize the x,y variables while the second ... constructor Rectangle initializes the width and height variables Here, we have both super() and this() are ... called, I hope you understood from this() and super() and also how to use them correctly in our program in ... the next course we will continue our lesson and at the end of the courses we will perform a challenge.