CodeGym /Java Course /Java Core /Interface tasks

Interface tasks

Java Core
Level 2 , Lesson 9
Available

"Hello, Amigo! Soon you'll be dreaming of interfaces. So I wouldn't sleep if I were you. No way! Ha, ha! Here are a couple of little exercises for you. May interfaces ever remain in your brain."

3
Task
Java Core, level 2, lesson 9
Locked
Wanna fly?
Rishi Gatesman, a CodeGym teacher, used to say that the interface is the legitimate child of Abstraction and Polymorphism. Let's start studying it. Have you ever flown in a dream? Have you ever dreamed of actually flying? Here's your task: implement the CanFly interface with two methods. Can you do it?
3
Task
Java Core, level 2, lesson 9
Locked
Fly, run and swim
It would be awesome to be Superman! He flies fast as lightning, outruns the wind, and swims like a fish. Wait, can Superman even swim? Whatever, doesn't matter. Write a generic template for a superhero that can fly, swim, and run. You need to do this by implementing the CanFly, CanRun, and CanSwim interfaces.
3
Task
Java Core, level 2, lesson 9
Locked
Eat, fly, and move
Some kind soul already wrote for you perfect interfaces for flying, moving, and eating. Let's add these interfaces to the dog, duck, car, and airplane classes. Let common sense be your guide as you dream up the solution. Sometimes common sense is a programmer's best friend.
3
Task
Java Core, level 2, lesson 9
Locked
Making a human
No fancy bells and whistles in this task. We simply need to correctly associate the CanFly, CanRun, and CanSwim interfaces with the Human, Duck, Penguin, and Airplane classes. Can you do it? Carry on! Don't know how? Go study the corresponding lesson, and then - carry on!
3
Task
Java Core, level 2, lesson 9
Locked
Human class and CanRun and CanSwim interfaces
Once again, like real programmers, we have to create a human. And, as always, we will accomplish this by writing code. Alas. We'll grant our human the Run and Swim interfaces, but we won't implement them. Because our man is abstract. Make sense? If not, it's time to review the lessons on abstract classes and interfaces.
Comments (16)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #11267629 (Anouk) Level 27, Netherlands Expert
28 April 2023
I have a general question about this topic. If you have a class Human (for example in the last exercise) implementing the interfaces, must such class always be abstract and public? Or could it be something else? Static if the interface is static? Thanks in advance!
Angel Li Level 18, Fremont, United States
29 June 2020
I have two questions: 1. How come interface methods can't have a body(the curly braces)? How do you execute things in a method then? 2. Why do methods of a class (the inherited abstract ones) need to be public? For example:

public interface CanFly {
        void fly();
    }
public class Airplane implements CanFly {
        public void fly() { /* needs to be public otherwise it says "attempting to assign weaker access privileges; was public" */
            
        }
    }
Thang Za Thang Level 18, Melbourne, Australia
7 July 2020
Answering 1: Interface is just saying, "Look who ever's gonna implement me, You need have these methods and you gotta write it yourself!". This is the point of interface! We'll see its uses later. Answering 2: public interface CanFly { void fly(); } You cant make the void fly() a private because it itself is a public. EVERY method in interface has a hidden "public abstract" so void fly() is actually "public abstract void fly();". When u make it private, u are literally attempting to assign a weaker access to it but you're the one implementing it. <s**t you're higher level than me but i already wrote this down so i won't be deleting >
Angel Li Level 18, Fremont, United States
12 July 2020
Lol thanks for clarifying though! Yeah if all the child classes all have the same method and you want to implement it in the parent class, using abstract classes work. Thanks!
Isaiah Burkes Level 16, Tampa, United States
22 December 2019
Still trying to wrap my head around how implementing the methods from an interface is going to be useful in the future since we can't write any actual code in the method.
Jarret Fawcett Level 12, Pittsburgh, United States
1 March 2020
It helps with flexibility in larger applications to create loosely coupled code. You can on the fly create a new class and implement the interface methods and deploy the software almost immediately with minimal changes to the application as a whole.
Isaiah Burkes Level 16, Tampa, United States
1 March 2020
Thanks!
22 July 2019
after the very last task I now find this very convenient :D
Ewerton Level 30, Belo Horizonte, Brasil
29 June 2019
You can make Intellij put the methods for you, just write the interfaces:

public class YourClass implements interfaceA, interfaceB{

}
Then press Ctrl + i.
Michael Level 24, Madrid, Spain
13 September 2021
Can you tell me waht is the shortcut? Thanks in advance
Krisztian Level 24, Budapest, Hungary
6 June 2019
So, because of the interfaces I have to write at least twice some method names (for example I write at the CanFly() interface the fly() method and again write it at the specific class, for example at the Airplane). Why on the Earth is it so useful?? Because for me it seems like a pre-note for myself to what should I write at some classes later. It seems for me redundant... :/
Kotlet Level 28, Wroclaw, Poland
26 June 2019
I was wondering about this too. But as I understand it (if I'm wrong please someone corrects me) it extends the power of polymorphism and gives you additonal "type" (role) of each object. For example if you have Animal [] array you can put in there Animal objects and every other object that extends from Animal Class (for example a Dog). But you cannot put something outside inheritance tree (for example a Chair - object which extends from different tree - let's say Furniture Class). However if the Dog and the Chair Classes implements fourLeg interface, even though they are different classes from different trees, Dog and Chair object can be put together in fourLeg [] array. This is because they have implemented common interface. Therefore Dog object is instance of Animal Class, Dog Class and fourLeg (interface), while Chair object is instance of Furniture Class, Chair class and fourLeg (interface). The same goes for methods (if you have method with fourLeg parameter (void doSomething(fourLeg a) {...}) you can pass there Dog object or Chair object). So probably with one inheritance tree interface is not so usefull but with many it is helpful.
Krisztian Level 24, Budapest, Hungary
27 June 2019
Yeah, now I get it, that it's a kind of solution for the "there is no multiple-inheritance in java" problem, because with interfaces you don't have to face with diamond-problem. but idk... it still feels like to me redundant. it's good, that there is a grouping solution with interfaces (like classes), therefore solves that kind of problem that is mentioned by yourself, but... I have to write the method names over and over again :D I guess something for something.
Kotlet Level 28, Wroclaw, Poland
27 June 2019
Well, as it is stated in article on the next page (which, when I replied, I did not know of), you can always put default method in interface which will save you some time. For me it was (and still is) also a little bit unintuitive. Let's hope that some day, it will be perfectly natural for us :)
Bert Doe Level 22, Flanders, Belgium
21 April 2019
moving on
JeRiF94 Level 22, Baku, Azerbaijan
15 April 2019
Done