OOP Midterm
Constructor - is used in the creation of an object that is an instance of a class using the new keyword.
If you do not include any constructors in a class, Java provides a default constructor, a constructor with an empty parameter list and body. This is invisibly added to the class. It is also known as a no-argument constructor.
this keyword - is used when the instance variable has the same name with the constructor’s parameter.
Constructor overloading - occurs when constructors have different type parameters.
constructor chaining - When a constructor calls another constructor with a greater number of parameters
Encapsulation - describes the ability of an object to hide its data and methods.
In Java, a class encapsulates the fields, which holds the state of an object, and the methods, which define the actions of the object.
An accessor or a getter method - is a public method that returns data from a private instance variable.
A mutator or a setter method - is a public method that changes the data stored in one or more private instance variables.
A class is considered immutable if it remains unchanged after an object of another class is constructed.
Inheritance allows a class to acquire all the attributes (fields) and behaviors (methods) of another class.
A class that is derived from another class is called a subclass. It is also known as derived class or child class.
The class from which the subclass is derived is called a superclass. It is also known as base class or parent class.
In Java, all classes inherit from the Object class. It is the only class that does not have any parent classes.
To prevent a class from being extended, mark the class with the final modifier.
Even though the superclass is public, its subclass cannot access its private fields.
Rules Define Constructor
· The first statement of every constructor is either a call to another constructor within the class using this() or a call to constructor in the direct parent class using super().
· The super() command may only be used as the first statement of the constructor.
· If a super() call is not declared in a constructor, Java will insert a no-argument super() as the first statement of the constructor.
· If the parent class does not have a no-argument constructor and the child class does not define any constructors, the compiler will throw an error. This is because the child class has an invisible no-argument super() that tries to call the constructor of the parent class.
· If the parent class does not have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor.
Calling Constructors and Overriding Methods
The parent constructor is always executed before the child constructor.
You can define a new version of an existing method in a childclass that makes use of the definition in the parent class. This ability is called method overriding.
To override a method, declare a new method with the same name, parameter list, and return type as the method in the parent class. The method in the subclass must be at least as accessible as the method in the parent class.