Extend the functionality

Our Objective 

Write a Java program that extends the functionality of the topic in addition to what is defined in a superclass related to overriding in an object-oriented program.


The Theory 

Method Overriding: 

Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The overridden method in the subclass should have the same method signature (method name, parameters, and return type) as the method in the superclass. 

Key points about method overriding: 

  • The method in the subclass must have the same method signature as the method in the superclass. 
  • The access level of the overriding method in the subclass cannot be more restrictive than the access level of the overridden method in the superclass (e.g., you can't make an overridden method "private" if it's "protected" in the superclass). 
  • The "@Override" annotation is used in Java to indicate that a method is intended to override a superclass method. It helps catch errors if the method signature doesn't match any method in the superclass. 

Extending Functionality (Inheritance): 

Inheritance is a mechanism that allows a new class (subclass or derived class) to inherit properties (fields and methods) from an existing class (superclass or base class). This enables you to reuse code and extend the functionality of the superclass. 

Key points about inheritance: 

  • The subclass uses the "extends" keyword to inherit from a superclass. 
  • The subclass automatically inherits all the non-private fields and methods from the superclass. 
  • The subclass can add additional fields and methods or override existing methods from the superclass. 

Sample implementation given below. 

  1. public class Point{ 
  2.  private int x; 
  3.  private int y; 
  4.  Point() { 
  5.   this.x = 0; 
  6.   this.y = 0; 
  7.  } 
  8.  Point(int xInit, int yInit) { 
  9.   this.x = xInit; 
  10.   this.y = yInit; 
  11.  } 
  12.  private void setX(int xCoord) { 
  13.   this.x = xCoord; 
  14.  } 
  15.  private void setY(int yCoord) { 
  16.   this.y = yCoord; 
  17.  } 
  18.  private int getX() { 
  19.   return this.x; 
  20.  } 
  21.  private int getY() { 
  22.   return this.y; 
  23.  } 
  24.  private void print() { 
  25.   System.out.println("(" + this.x + "," + this.y+ ")"); 
  26.  } 
  27.  private boolean isOrigin() { 
  28.   if((this.x==0)&&(this.y==0)) 
  29.    return true; 
  30.   else 
  31.    return false; 
  32.  } 
  33. public class Point3D extends Point{ 
  34.  protected int z; 
  35.  public void setZ(int zCoord){ 
  36.   this.z = zCoord; 
  37.  } 
  38.  public int getZ(){ 
  39.   return this.z; 
  40.  } 
  41.  public boolean isOrigin() { 
  42.   if (super.isOrigin() && this.z == 0) 
  43.    return true; 
  44.   else 
  45.    return false; 
  46.  } 
  47. public class Driver { 
  48.  public static void main(String[]){ 
  49.   Point p = new Point(); 
  50.   Point3D p3 = new Point3D(); 
  51.   p.isOrigin(); 
  52.   p3.setZ(0); 
  53.   p3.isOrigin(); 
  54.  } 

The provided code consists of three classes: Point, Point3D, and Driver. Let's break down the algorithm of the program step by step: 

  • Point Class: Point is a class that represents a 2D point with x and y coordinates. 
  • It has two constructors: a default constructor that initializes x and y to 0, and a parameterized constructor that takes initial x and y values. 
  • Private methods setX and setY allow setting individual coordinates. 
  • Private methods getX and getY return the current x and y coordinates. 
  • Private method print is used to print the coordinates in the format "(x, y)". 
  • Private method isOrigin checks whether the point is at the origin (0, 0). 
  • Point3D Class: Point3D is a subclass of Point that adds a third coordinate z to represent a point in 3D space. 
  • It has a public method setZ to set the z coordinate and a public method getZ to retrieve it. 
  • The class overrides the isOrigin method to check if the point is at the origin (0, 0, 0) in 3D space. 
  • Driver Class: Driver contains the main method where program execution begins. 
  • Inside main, an instance of Point called p is created using the default constructor. 
  • An instance of Point3D called p3 is also created. 
  • The isOrigin method of p is called, but its return value is not used. 
  • The setZ method of p3 is called to set the z coordinate to 0. 
  • The isOrigin method of p3 is called, and its return value is not used. 


Learning Outcomes 

  • Understanding Inheritance: Students will grasp the concept of inheritance and how it facilitates code reuse by allowing a subclass to inherit properties and methods from its superclass. 
  • Comprehending Subclass and Superclass Relationship: Students will be able to define the relationship between a subclass and its superclass, recognizing that a subclass is a specialized version of its superclass. 
  • Implementing Method Overriding: Students will learn how to override methods in a subclass to provide their own implementations, taking advantage of polymorphism to treat objects of different classes uniformly through a common interface.