title-img


Java Inheritance II

Write the following code in your editor below: 1.A class named Arithmetic with a method named add that takes integers as parameters and returns an integer denoting their sum. 2.A class named Adder that inherits from a superclass named Arithmetic. Your classes should not be be public. Input Format You are not responsible for reading any input from stdin; a locked code stub will test your submission by calling the add method on an Adder object and passing it 2 integer parameters. Outp

View Solution →

Java Abstract Class

A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge. Following is an example of abstract class: abstract class Book{ String title; abstract void setTitle(String s); String getTitle(){ return title; } } If you try to create an instance of this class like the following line you will g

View Solution →

Java Interface

A Java interface can only contain method signatures and fields. The interface can be used to achieve polymorphism. In this problem, you will practice your knowledge on interfaces. You are given an interface AdvancedArithmetic which contains a method signature int divisor_sum(int n). You need to write a class called MyCalculator which implements the interface. divisorSum function just takes an integer as input and return the sum of all its divisors. For example divisors of 6 are 1, 2, 3 and 6,

View Solution →

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 →