title-img


Java Method Overriding

When a subclass inherits from a superclass, it also inherits its methods; however, it can also override the superclass methods (as well as declare and implement new ones). Consider the following Sports class: class Sports{ String getName(){ return "Generic Sports"; } void getNumberOfTeamMembers(){ System.out.println( "Each team has n players in " + getName() ); } } Next, we create a Soccer class that inherits from the Sports class. We can override the g

View Solution →

Java Method Overriding 2 (Super Keyword)

When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword. If you write super.func() to call the function func(), it will call the method that was defined in the superclass. You are given a partially completed code in the editor. Modify the code so that the code prints the following text: Hello I am a motorcycle, I am a cycle with an engine. My ancestor is a cycle who is a vehicle with pedals.

View Solution →

Java Instanceof keyword

The Java instanceof operator is used to test if the object or instance is an instanceof the specified type. In this problem we have given you three classes in the editor: Student class Rockstar class Hacker class In the main method, we populated an ArrayList with several instances of these classes. count method calculates how many instances of each type is present in the ArrayList. The code prints three integers, the number of instance of Student class, the number of instance of Rocksta

View Solution →

Java Iterator

Java Iterator class can help you to iterate through every element in a collection. Here is a simple example: import java.util.*; public class Example{ public static void main(String []args){ ArrayList mylist = new ArrayList(); mylist.add("Hello"); mylist.add("Java"); mylist.add("4"); Iterator it = mylist.iterator(); while(it.hasNext()){ Object element = it.next(); System.out.println((String)element);

View Solution →

Java Exception Handling (Try-catch)

Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. (Wikipedia) Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs. This problem will test your knowledge on try-catch block. You will

View Solution →