-
Notifications
You must be signed in to change notification settings - Fork 0
inheritance #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
inheritance #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| <exclude-output /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
| </content> | ||
| <orderEntry type="jdk" jdkName="openjdk-24" jdkType="JavaSDK" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| </component> | ||
| </module> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class CookSpongebob extends Spongebob{ | ||
|
|
||
| private int age; | ||
| private String favoriteFood; | ||
|
|
||
| public CookSpongebob(int age, String favoriteFood) { | ||
| super("Cook Spongebob",age,favoriteFood); | ||
| super.playBasketBall(); | ||
| } | ||
|
|
||
| @Override | ||
| public void playBasketBall() { | ||
| System.out.println("Why would I play basketball, Cooking is better"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| public abstract class Fish { | ||
|
|
||
| private String name; | ||
| private int age; | ||
| private String favoriteFood; | ||
|
|
||
| public Fish (String name, int age, String favoriteFood) { | ||
| this.name = name; | ||
| this.age = age; | ||
| this.favoriteFood = favoriteFood; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getAge() { | ||
| return age; | ||
| } | ||
|
|
||
| public void setAge(int age) { | ||
| this.age = age; | ||
| } | ||
|
|
||
| public String getFavoriteFood() { | ||
| return favoriteFood; | ||
| } | ||
|
|
||
| public void setFavoriteFood(String favoriteFood) { | ||
| this.favoriteFood = favoriteFood; | ||
| } | ||
|
|
||
| public void takeTheCar() { | ||
| System.out.println("Taking car"); | ||
| } | ||
| public void takeTheTrain() { | ||
| System.out.println("Taking train"); | ||
| } | ||
| public void walkByFoot() { | ||
| System.out.println("Walking by foot"); | ||
| } | ||
|
|
||
| public abstract void goToWork(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or | ||
| // click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| //TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text | ||
| // to see how IntelliJ IDEA suggests fixing it. | ||
| System.out.printf("Hello and welcome!"); | ||
|
|
||
| for (int i = 1; i <= 5; i++) { | ||
| //TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint | ||
| // for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>. | ||
| System.out.println("i = " + i); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class MrCrab extends Fish{ | ||
|
|
||
| public MrCrab(String name, int age, String favoriteFood) { | ||
| super("Mr Crab",age,favoriteFood); | ||
| } | ||
|
|
||
| @Override | ||
| public void goToWork(){ | ||
| takeTheTrain(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Patrick extends Fish implements ProfessionalBasketBallPlayer{ | ||
|
|
||
| public Patrick(String name, int age, String favoriteFood) { | ||
| super("Patrick",age,favoriteFood); | ||
| } | ||
|
|
||
| @Override | ||
| public void goToWork() { | ||
| walkByFoot(); | ||
| } | ||
|
|
||
| @Override | ||
| public void playBasketBall() { | ||
| System.out.println("Ohh an orange ball lets eat it"); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| public interface ProfessionalBasketBallPlayer { | ||
|
|
||
| public void playBasketBall(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Spongebob extends Fish implements ProfessionalBasketBallPlayer{ | ||
|
|
||
| public Spongebob(String name, int age, String favoriteFood) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you get a name if you don't use it? |
||
| super("Spongebob",age, favoriteFood); | ||
| } | ||
|
|
||
| @Override | ||
| public void goToWork() { | ||
| takeTheCar(); | ||
| } | ||
|
|
||
| @Override | ||
| public void playBasketBall() { | ||
| System.out.println("Ohh a cool orange ball and it is bouncy"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||||||
| public class Util { | ||||||||||
|
|
||||||||||
| public static void professionalBasketBall(ProfessionalBasketBallPlayer[] basketBallPlayers) { | ||||||||||
| for (ProfessionalBasketBallPlayer p : basketBallPlayers) { | ||||||||||
| if(p instanceof Fish basketBallFish) { | ||||||||||
| basketBallFish.goToWork(); | ||||||||||
| p.playBasketBall(); | ||||||||||
| } | ||||||||||
| else { | ||||||||||
| p.playBasketBall(); | ||||||||||
|
Comment on lines
+7
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try not to copy code, call playBasketball only once |
||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public static void professionalBasketBallArr(Fish[] fish) { | ||||||||||
| int counter = 0; | ||||||||||
| int index = 0; | ||||||||||
| for (Fish f : fish) { | ||||||||||
| if(f instanceof ProfessionalBasketBallPlayer) { | ||||||||||
| counter++; | ||||||||||
| } | ||||||||||
| ProfessionalBasketBallPlayer[] basketBallPlayers = new ProfessionalBasketBallPlayer[counter]; | ||||||||||
| for (Fish fp : fish) { | ||||||||||
| if(fp instanceof ProfessionalBasketBallPlayer) { | ||||||||||
| basketBallPlayers[index] = (ProfessionalBasketBallPlayer)fp; | ||||||||||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
no need for casting |
||||||||||
| index++; | ||||||||||
| } | ||||||||||
| professionalBasketBall(basketBallPlayers); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| } | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this because fish already has this and this class inherits from fish