OOP using Java13.0
So far, we have been examining the concept of Object-Oriented Programming (OOP) as a programming paradigm which is centered on objects of class blocks (or segments). Constructors are special methods which are used to construct an object, that is essentially an instance, of a class block.
this
keyword
- A virtual object-reference, used during the implementation of a class block, to point (or refer) to only instance-based members (Constants, Variables, Methods, Constructors, etc.) resident in the class block.
- Within a given Constructor, it can be used to call (or invoke) one or more other Constructor(s) defined within the same class block.
- When using
this(...)
, within a given Constructor, to invoke another Constructor; it must be the first (1st) statement of code within the given Constructor.
The following code snippets highlight the various approaches, that can be employed using the
this
keyword within Constructor(s), toward calling or invoking other Constructor(s), viz:
- Calling a Constructor possessing no parameter, i.e. DEFAULT Constructor:
this()
- Calling a Constructor possessing one (1) parameter, i.e. CUSTOM Constructor or COPY Constructor:
this(<argument>)
- Calling a Constructor possessing two (2) parameters, i.e. CUSTOM Constructor or COPY Constructor:
this(<argument1>, <argument2>)
, and so on.
Moreover, in the following code snippet, the invocation of Constructor(s) was/were done via the
this()
keyword.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Person { //"Container" class
private String pName;
private static String pType;
//CUSTOM Constructor
public Person (String pName, String pType) {
this.pName = pName; //Set the instance-based variable
Person.pType = pType; //Set the static-based variable
}
//DEFAULT Constructor
public Person () {
//Call to CUSTOM Constructor (of same class block) using: this()
this("Undefined Name", "Undefined Type"); //MUST be 1st code statement here
//Accessing instance-based variable using: this object-reference
System.out.print("pName: " + this.pName);
//Accessing static-based variable
System.out.println(", pType: " + Person.pType);
}
}
Container class: Designing and implementing an OOP class which invokes Constructor(s) using the this()
keyword
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class PersonDriver { //"Driver" class
public static void main(String[] args) {
//Call to DEFAULT Constructor: Instantiate objects of the "Person" class block
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
}
}
Driver class: Designing and implementing an OOP class which creates instances (or objects) of the "Person" class
13.1Object
Superclass in Java Programming
Caption: Object
superclass in Java
This is a special type of class (block) which exists in Java Programming Language; and the
Object
superclass possesses the following characteristics:
- It is a universal class and a superclass which exists in Java with respect to Object-Oriented Programming.
- Every class block implemented in Java Programming Language essentially inherits from the
Object
superclass. In other words, every class (block) implemented in Java automatically becomes a subclass to the Object
superclass.
- In this regard, every class (block) implemented in Java can literally call or reference all the non-
private
members (Constants, Variables, Methods, etc.) of the Object
superclass.
- The
Object
superclass is predefined within and/or resident in the following Java (Library) package: java.lang
. Explicitly, it can be accessed via: java.lang.Object
- The
Object
superclass possesses a public
access modifier.
- Its official documentation can be accessed by clicking here.
Furthermore, the following non-
private
methods have already been implemented within the
Object
superclass, namely:
clone()
: Creates and returns a copy of the referenced object.
equals(Object <obj>)
: Indicates whether a Caller object (this object) is equal to the object (some other object) being passed as an argument.
toString()
: Returns a string representation of the referenced object.
finalize()
: Called by the Garbage Collector (resident in the Java Virtual Machine) on an object whenever the garbage collection process determines that there are no more references to the object.
getClass()
: Returns the runtime class/type of the referenced object.
hashCode()
: Returns a hash code value for the object.
notify()
: Wakes up a single thread that is waiting on the referenced object's monitor.
notifyAll()
: Wakes up all threads that are waiting on the referenced object's monitor.
wait()
: Causes the current thread to wait until another thread invokes the notify()
method or the notifyAll()
method on the referenced object.
wait(long <timeout>)
: Causes the current thread to wait until either another thread invokes the notify()
method or the notifyAll()
method on the referenced object, or a specified amount of time has elapsed.
wait(long <timeout>, int <nanos>)
: Causes the current thread to wait until another thread invokes the notify()
method or the notifyAll()
method on the referenced object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
• To this end, when we had earlier (in previous lessons/chapters) used the
clone()
method to implement a
deep copy (e.g.
int[] cloneArr = dataArr.clone()
) with respect to Arrays and ArrayLists; we were essentially calling the
clone()
method which is originally implemented in the
Object
superclass, because all methods of the
Object
superclass can be directly called, invoked, or implemented by any other class (block) within Java.
• Also, in previous lessons, where we had to compare whether two (2)
String
values were equal using the
equals()
method: e.g.
str1.equals(str2)
; technically, we were simply calling (or invoking) the
equals(Object <obj>)
method of the
Object
superclass.
• Moreover, whenever we try to
parse an object to its corresponding
String
value (in Java Programming) using the
toString()
method: e.g.
Integer m1 = 4567; String m2 = m1.toString()
; we are essentially calling or invoking the
toString()
method of the
Object
superclass.
Exercise 1: Design and develop a Java program, such that it employs the concepts of Object-Oriented Programming (OOP), toward implementing an array of
Student
objects with respect to students registration in a course.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Student { //"Container" class
private String fName;
private String lName;
private int regNum;
//DEFAULT Constructor
public Student () {
System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
}
//CUSTOM Constructor
public Student (String firN, String lasN, int regN) {
this.fName = firN;
this.lName = lasN;
this.regNum = regN;
System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
}
//Method (Accessor/Getter)
public String returnStud () {
return this.regNum + " (" + this.fName + ", " + this.lName + ")";
}
}
Container class: Designing and implementing an OOP class identified as "Student" class
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class StudentDriver { //"Driver" class
public static void main(String[] args) {
//Creating an array of "Student" objects
Student[] obj = new Student[3];
//Instantiating the array of "Student" objects to non-null values
obj[0] = new Student ("Jane", "Doe", 10002000);
obj[1] = new Student ("Alpha", "Bravo", 30004000);
obj[2] = new Student ("Quebec", "Zulu", 50006000);
//Create a Deep-Copy of the "obj" array
Student[] copyObj = obj.clone();
//Update (via reconstruction) some elements of the "obj" objects' array
obj[0] = new Student ("Delta", "Echo", 70001000);
obj[2] = new Student ("Romeo", "Sierra", 80002000);
//Display the object-references of both "obj" array and "copyObj" array
System.out.println();
System.out.println("Object-Reference for 'obj1' = " + obj);
System.out.println("Object-Reference for 'copyObj' = " + copyObj);
//Display the contents of both "obj" array and "copyObj" array
System.out.println();
System.out.println("S/N: \t 'obj' Array of objects \t 'copyObj' Array of objects");
for (int i=0; i<obj.length; ++i) {
String objValue = obj[i].returnStud();
String copyObjValue = copyObj[i].returnStud();
System.out.println(i+1 + ": \t " + objValue + " \t " + copyObjValue);
}
}
}
Driver class: Designing and implementing an OOP class, which implements a Deep Copy, with respect to an Array of objects
13.2Overriding with respect to Object-Oriented Programming
In a real-world classroom scenario, when we write a textual definition on a piece of paper; and thereafter, we wish to make some correction(s) and/or update(s) to this definition, we can simply
erase and
rewrite the definition or we can just
overwrite (write over) the earlier definition. Thus, the idea of
Overriding in Object-Oriented Programming (OOP) can be conceptualized as a
rewrite procedure or an
overwrite procedure.
Overriding, with respect to Object-Oriented Programming, is a process whereby a subclass (or child class) re-implements a method or function originally defined and implemented in a superclass (or parent class). In the process of
overriding a method or function of a superclass, from within a subclass (or child class); the overridden method's re-implementation, resident within the subclass, possesses exactly the same method header or signature (i.e. possessing exactly same method name and same parameter types) as defined in the superclass (or parent class).
In this regard, consider the following Java program with respect to the functions and/or outputs of the
toString()
and the
equals(Object <obj>)
methods; which have both been originally defined and implemented in the
Object
superclass. Consequently, we shall override the implementation of both methods, the
toString()
and the
equals(Object <obj>)
, from within a child class.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
class Student { //"Container" class with package-private access modifier
private String fName;
private String lName;
private int regNum;
//DEFAULT Constructor
public Student () {
System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
}
//CUSTOM Constructor
public Student (String firN, String lasN, int regN) {
this.fName = firN;
this.lName = lasN;
this.regNum = regN;
System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
}
}
public class StudentDriver { //"Driver" class with public access modifier
public static void main(String[] args) {
//Create a new object (or instance) of the "Student" class
Student obj1 = new Student();
//Invoking the toString() method of the "Object" superclass
System.out.println("toString() method of 'Object' superclass: " + obj1.toString());
/*
* Alternatively, we can use this approach because the print() and println() methods
* automatically call the .toString() method of "Object" superclass
*/
System.out.println("toString() method of 'Object' superclass: " + obj1);
//Create another new object (or instance) of the "Student" class
Student obj2 = new Student("Alpha", "Bravo", 20003000);
//Invoking the equals(Object <obj>) method of the "Object" superclass
boolean b1 = obj2.equals(obj1);
System.out.println("equals(Object <obj>) method of 'Object' superclass: " + b1);
}
}
Container class and Driver class: Program employing the default functions of the toString()
method and the equals(Object <obj>)
method
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
class Student { //"Container" class with package-private access modifier
private String fName;
private String lName;
private int regNum;
//DEFAULT Constructor
public Student () {
System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
}
//CUSTOM Constructor
public Student (String firN, String lasN, int regN) {
this.fName = firN;
this.lName = lasN;
this.regNum = regN;
System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
}
//1: Overridden Method in "Student" subclass
public String toString () {
return "Overriding/Overwriting Object.toString() method!!";
}
//2: Overridden Method in "Student" subclass
public boolean equals (Object anotherObj) {
return true; //Always returns TRUE, irrespective of any comparison verdict
}
}
public class StudentDriver { //"Driver" class with public access modifier
public static void main(String[] args) {
//Create a new object (or instance) of the "Student" class
Student obj1 = new Student();
//OVERRIDE: Invoking the toString() method of the "Student" subclass
System.out.println("Overriding toString() of 'Object' superclass: " + obj1.toString());
/*
* Alternatively, we can use this approach because the print() and println() methods
* automatically call the .toString() method of "Object" superclass
*/
System.out.println("Overriding toString() of 'Object' superclass: " + obj1);
//Create another new object (or instance) of the "Student" class
Student obj2 = new Student("Alpha", "Bravo", 20003000);
//OVERRIDE: Invoking the equals(Object <obj>) method of the "Student" subclass
boolean b1 = obj2.equals(obj1);
System.out.println("Overriding equals(Object <obj>) of 'Object' superclass: " + b1);
}
}
Container class and Driver class: Program employing the overridden functions of the toString()
method and the equals(Object <obj>)
method
13.3Practice Exercise
-
With respect to the following Java program in OOP paradigm, do you think that this program will compile successfully and run successfully too?
If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Combo {
private String cName;
private static String cType;
public Combo () {
System.out.println("cName = " + this.cName + ", cType = " + Combo.cType);
}
public Combo (String cName, String cType) {
this.cName = cName;
Combo.cType = cType;
}
public static void main(String[] args) {
Combo c1 = new Combo();
Combo c2 = new Combo("OOP in Java", "Course Title");
System.out.println("cName = " + c2.cName);
System.out.println("cType = " + Combo.cType);
}
}
-
Given the following program with respect to OOP paradigm, do you think that this program will compile successfully and execute successfully too?
If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Combo {
private String cName;
private static String cType;
public Combo (String cName, String cType) {
this.cName = cName;
Combo.cType = cType;
}
public static void main(String[] args) {
Combo c1 = new Combo();
}
}
-
Do you think that the program below will compile successfully and run successfully?
If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Combo {
private String cName;
private static String cType;
public static void main(String[] args) {
Combo c1 = new Combo();
System.out.println("cName = " + c1.cName);
System.out.println("cType = " + Combo.cType);
}
}
-
Do you think that the program below will compile successfully and run successfully?
If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Combo {
private String cName;
private static String cType;
public Combo (String cName, String cType) {
this.cName = cName;
Combo.cType = cType;
}
public static void main(String[] args) {
Combo c1 = new Combo("Programming in Java", "Course Name");
System.out.println("cName = " + c1.cName);
System.out.println("cType = " + Combo.cType);
}
}
-
Given the following Java program with respect to OOP paradigm, do you think that this program will compile successfully and run successfully too?
If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Combo {
private String cName;
private static String cType;
public Combo () {
System.out.println("cName = " + this.cName + ", cType = " + Combo.cType);
}
public Combo (String cName, String cType) {
this.cName = cName;
Combo.cType = cType;
}
public static void main(String[] args) {
Combo c1 = new Combo("Alpha", "Zulu");
Combo c2 = new Combo("Bravo", "Yankee");
Combo c3 = new Combo("Charlie", "X-ray");
System.out.println("cName = " + c2.cName);
System.out.println("cType = " + Combo.cType);
}
}
-
With respect to the following program in OOP paradigm, do you think that this program will compile successfully and run successfully too?
If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
/**
* @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
* @course ProgrammingLessons (proglessons).
* @title OOP using Java.
* @author Dr. Bonaventure Chidube Molokwu.
*/
public class Combo {
private String cName;
private String cType;
public Combo () {
System.out.println("cName = " + this.cName + ", cType = " + this.cType);
}
public Combo (String cName, String cType) {
this.cName = cName;
this.cType = cType;
}
public String toString () {
return "cName, cType";
}
public static void main(String[] args) {
Combo c1 = new Combo("Alpha", "Zulu");
Combo c2 = new Combo("Bravo", "Quebec");
System.out.println("cName = " + c1.cName);
System.out.println("cType = " + c1.cType);
System.out.println("cName = " + c2.cName);
System.out.println("cType = " + c2.toString());
}
}
- TRUE or FALSE: The
Object
class is a subclass?
- TRUE or FALSE: With respect to the concepts of Object-Oriented Programming (OOP), during the course of implementing method
Overriding
, the re-implemented method in the subclass can possess a different method header?
- TRUE or FALSE: A superclass can inherit the attributes as well as the functions of a subclass?
- TRUE or FALSE: The
this
keyword cannot be used to invoke a Constructor with respect to a class?