Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Java] I\'m kinda confused about what \'overriding\' is. I thought using overrid

ID: 3582150 • Letter: #

Question

[Java]

I'm kinda confused about what 'overriding' is. I thought using override when we have several methods with same name but different variable name or type. But I can't fine any same method name while it has @override.

import java.util.ArrayList;
/**
* Models a list of vocabulary words
*/
public class Vocab
{
    // instnace variables of the data
    private ArrayList words;
    /**
     * Constructor that take array list as a parameter
     * @param theList array list variable
     */
    public Vocab(ArrayList theList)
    {
        words = theList; // getting the data to the varaible
    }
    /**
     * Removes occurrence of the given word
     * @param target the word given to all occurrence be removed
     */
    public void remove(String target)
    {
        for(int i =0; i < words.size() - 1; i++)
        {
            String wordToTest = words.get(i);
            if (wordToTest.contains(target))
            {
                words.remove(target);
            }
        }
    }
    /**
     * Get the longest word
     * @return longest word in the array
     */
    public String longest()
    {
        if(words.size() < 1)
        {
            return null;
        }
        else
        {
            String longestWord = words.get(0);
            for(int i = 1; i < words.size(); i++)
            {
                String wordToTest = words.get(i);
                if(wordToTest.length() > longestWord.length())
                {
                    longestWord = wordToTest;
                }
            }
            return longestWord;
        }
    }
    /**
     * Get the average length of the word
     * @return average length of the words
     */
    public double averageLength()
    {
        if (words.size() < 1)
        {
            return 0;
        }
        else
        {
            double totalLengths = 0;
            for(int i = 0; i < words.size(); i++)
            {
                String wordToTest = words.get(i);
                totalLengths = totalLengths + wordToTest.length();
            }
            return totalLengths / (words.size());
        }
    }
    /**
     * Returns a string representation
     * @return string of the list
     */
    @Override
    public String toString()
    {
        return words.toString();
    }
}

import java.util.ArrayList;

public class VocabTester
{

    public static void main(String[] args)
    {
        ArrayList theList = new ArrayList<>();
        Vocab empty = new Vocab(theList);
        System.out.println(empty);
        System.out.println("Expected: []");
       
        theList.add("polymorphism");
        theList.add("interface");
        theList.add("encapsulation");
        theList.add("interface");
        theList.add("object-oriented");
        theList.add("java");
        theList.add("programming");
       
        Vocab vocab = new Vocab(theList);
        vocab.remove("interface");
        System.out.println(vocab);
        System.out.println("Expected: [polymorphism, interface, encapsulation, interface, object-oriented, java, programming]");
        System.out.println(vocab.longest());
        System.out.println("Expected: null");
        System.out.println(vocab.averageLength());
        System.out.println("Expected: 0.0");
    }
}

Explanation / Answer

so,I think you got confused between override and overload.

Overload is having same function name with either differnt no of argumetns or differnt types of parameters

On the other hand, override is having same function name and se paramaters. So,here the function prototype won't be chnaged.

Hence, in the abov example toString method that you see is a perfect example of this. We aren't changing the prototype of the funciton but just modifying the method logic

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote