Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/inheritance/inheritance.iml
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>
15 changes: 15 additions & 0 deletions src/inheritance/src/CookSpongebob.java
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;
Copy link

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


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");
}
}
48 changes: 48 additions & 0 deletions src/inheritance/src/Fish.java
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();
}
15 changes: 15 additions & 0 deletions src/inheritance/src/Main.java
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);
}
}
}
11 changes: 11 additions & 0 deletions src/inheritance/src/MrCrab.java
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();
}
}
17 changes: 17 additions & 0 deletions src/inheritance/src/Patrick.java
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");
}

}
4 changes: 4 additions & 0 deletions src/inheritance/src/ProfessionalBasketBallPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface ProfessionalBasketBallPlayer {

public void playBasketBall();
}
16 changes: 16 additions & 0 deletions src/inheritance/src/Spongebob.java
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) {
Copy link

Choose a reason for hiding this comment

The 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");
}
}
33 changes: 33 additions & 0 deletions src/inheritance/src/Util.java
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
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(fp instanceof ProfessionalBasketBallPlayer) {
basketBallPlayers[index] = (ProfessionalBasketBallPlayer)fp;
if(fp instanceof ProfessionalBasketBallPlayer basketballPlayer) {
basketBallPlayers[index] = basketballPlayer;

no need for casting

index++;
}
professionalBasketBall(basketBallPlayers);
}
}

}
}