Skip to content

Commit 1e0486a

Browse files
himanshugoyal1065jonathanyeong
authored andcommitted
Implementing inheritance logic in Java
* Setting up the template * Adding the module for inheritance * Basic file structure copied from other module * Updating introduction.md and introductions.md files * Implemented the source code and test cases * Implemented the source code and test cases * Implemented the source code and test cases * Final Iteration. * [Ruby] Normalize naming for method references * Normalize naming for method references * Add some missed methods in the instructions.md * [Inheritance] Final Iteration 2 * Setting up the template * Adding the module for inheritance * Basic file structure copied from other module * Updating introduction.md and introductions.md files * Implemented the source code and test cases * Implemented the source code and test cases * Implemented the source code and test cases * Final Iteration. * [Inheritance] Final Iteration 2 * [Inheritance] adding to config.json file * [Inheritance] adding to config.json file * [Inheritance] adding to config.json file * Adding gradle-ci.yml for gradle check * Testing gradle CI github action. * Testing gradle CI github action. Co-authored-by: Jonathan Yeong <jonathanyeong@gmail.com>
1 parent 80e9c4d commit 1e0486a

11 files changed

Lines changed: 451 additions & 1 deletion

File tree

exercises/concept/classes/src/test/java/RemoteControlCarTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import org.junit.Assert;
21
import org.junit.Ignore;
32
import org.junit.Test;
43

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Inheritance
2+
3+
Java supports inheritance as it's core functionality and then to achieve a lot of OOPs principles like abstraction using
4+
inheritance.
5+
6+
A class can extend another class using `extends` keyword and can inherit from an interface using `implements` keywords.
7+
8+
## Access Modifiers
9+
10+
The access modifiers define rules for the member variables of a class about their access from another classes(or anywhere in
11+
the code).
12+
13+
There are mainly three access modifiers:
14+
15+
- private
16+
- public
17+
- protected
18+
- Default (No keyword required)
19+
20+
You can read more about them [here][access-modifiers]
21+
22+
## Inheritance vs Composition
23+
24+
These concepts are very similar and are often confused.
25+
26+
- Inheritance means that the child has IS-A relationship with the parent class.
27+
28+
```java
29+
interface Animal() {
30+
public void bark();
31+
}
32+
33+
class Dog implements Animal {
34+
public void bark() {
35+
System.out.println("Bark");
36+
}
37+
}
38+
```
39+
40+
Here, `Dog` IS-A `Animal`
41+
42+
- Composition represents a HAS-A relationship.
43+
44+
```java
45+
interface Engine {
46+
47+
}
48+
49+
class Car {
50+
private Engine engine;
51+
}
52+
```
53+
54+
Here, `Car` HAS-A `Engine`
55+
56+
[access-modifiers]: https://www.geeksforgeeks.org/access-modifiers-java/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## General
2+
3+
Detailed explanation of inheritance can be found at [Inheritance][inheritance-main].
4+
The whole inheritance concept has a lot to do with the concepts around [overriding][java-overriding].
5+
6+
## 1. Describe a Fighter.
7+
8+
- In Java, the `toString()` method is actually present inside the Object class (which is a superclass to all the classes in Java).
9+
You can read more about it [here][object-class-java].
10+
11+
- To override this method inside your implementation class, you should have a method with same name i.e `toString()` and same return type
12+
i.e `String`.
13+
14+
## 2. Making Fighters not vulnerable by default.
15+
16+
- Consider having a method `isVulnerable()` inside the `Fighter` class which states vulnerability of the fighter, return `false` to make it non-vulnerable by default.
17+
- This can than be overridden by any child class(the class extending `Fighter`), according to its requirements.
18+
19+
- Again the [overriding][java-overriding] concept will come handy.
20+
21+
## 3. Allowing wizards to prepare a spell.
22+
23+
- Preparing a spell can only be done by a wizard. So, it makes sense to have this property defined inside the `Wizard` class.
24+
25+
- Create `prepareSpell()` method inside `Wizard` class.
26+
27+
- Remember : Parent class(here `Fighter`) has no access to the properties of the child class(for example, `Wizard`)
28+
29+
## 4. Make Wizards vulnerable when not having prepared a spell
30+
31+
- Override the `isVulnerable()` method in the `Wizard` class to make Wizards vulnerable if they haven't prepared a spell.
32+
33+
## 5. Calculate the damage points for a Wizard
34+
35+
- Use a [conditional statement][if-else] to return the damage points, taking into account the value of the prepare spell field.
36+
37+
## 6. Calculate the damage points for a Warrior
38+
39+
- Use a [conditional statement][if-else] to return the the damage points, taking into account the vulnerability of the target.
40+
41+
[inheritance-main]: https://www.geeksforgeeks.org/inheritance-in-java/
42+
[object-class-java]: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
43+
[java-overriding]: https://docs.oracle.com/javase/tutorial/java/IandI/override.html
44+
[if-else]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
In this exercise you're playing a role-playing game named "Wizard and Warriors," which allows you to play as either a Wizard or a Warrior.
2+
3+
There are different rules for Warriors and Wizards to determine how much damage points they deal.
4+
5+
For a Warrior, these are the rules:
6+
7+
- Deal 6 points of damage if the fighter they are attacking is not vulnerable
8+
- Deal 10 points of damage if the fighter they are attacking is vulnerable
9+
10+
For a Wizard, these are the rules:
11+
12+
- Deal 12 points of damage if the Wizard prepared a spell in advanced
13+
- Deal 3 points of damage if the Wizard did not prepare a spell in advance
14+
15+
In general, fighters are never vulnerable. However, Wizards _are_ vulnerable if they haven't prepared a spell.
16+
17+
You have six tasks that work with Warriors and Wizard fighters.
18+
19+
## 1. Describe a fighter
20+
21+
Override the `toString()` method on the `Fighter` class to return a description of the fighter, formatted as `"Fighter is a <FIGHTER_TYPE>"`.
22+
23+
```java
24+
Fighter warrior = new Warrior();
25+
warrior.toString();
26+
// => "Fighter is a Warrior"
27+
```
28+
29+
## 2. Make fighters not vulnerable by default
30+
31+
Ensure that the `Fighter.isVulnerable()` method always returns `false`.
32+
33+
```java
34+
Fighter warrior = new Warrior();
35+
warrior.isVulnerable();
36+
// => false
37+
```
38+
39+
## 3. Allow Wizards to prepare a spell
40+
41+
Implement the `Wizard.prepareSpell()` method to allow a Wizard to prepare a spell in advance.
42+
43+
```java
44+
Wizard wizard = new Wizard();
45+
wizard.prepareSpell();
46+
```
47+
48+
## 4. Make Wizards vulnerable when not having prepared a spell
49+
50+
Ensure that the `isVulnerable()` method returns `true` if the wizard did not prepare a spell; otherwise, return `false`.
51+
52+
```java
53+
Fighter wizard = new Wizard();
54+
wizard.isVulnerable();
55+
// => true
56+
```
57+
58+
## 5. Calculate the damage points for a Wizard
59+
60+
Implement the `Wizard.damagePoints()` method to return the damage points dealt: 12 damage points when a spell has been prepared, 3 damage points when not.
61+
62+
```java
63+
Wizard wizard = new Wizard();
64+
Warrior warrior = new Warrior();
65+
66+
wizard.prepareSpell();
67+
wizard.damagePoints(warrior);
68+
// => 12
69+
```
70+
71+
## 6. Calculate the damage points for a Warrior
72+
73+
Implement the `Warrior.damagePoints()` method to return the damage points dealt: 10 damage points when the target is vulnerable, 6 damage points when not.
74+
75+
```java
76+
Warrior warrior = new Warrior();
77+
Wizard wizard = new Wizard();
78+
79+
warrior.damagePoints(wizard);
80+
// => 10
81+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Inheritance is a core concept in OOP (Object Oriented Programming). It donates IS-A relationship.
2+
It literally means in programming as it means in english, inheriting features from parent(in programming features is normally functions
3+
and variables).
4+
5+
Consider a class, `Animal` as shown,
6+
7+
```java
8+
//Creating an Animal class with bark() as a member function.
9+
public class Animal {
10+
11+
public void bark() {
12+
System.out.println("This is a animal");
13+
}
14+
15+
}
16+
```
17+
18+
`Animal` is a parent class, because the properties this class has can be extended to all the animals in general.
19+
20+
Consider an animal named `Lion`, having a class like,
21+
22+
```java
23+
//Lion class is a child class of Animal.
24+
public class Lion extends Animal {
25+
26+
public void bark() {
27+
System.out.println("Lion here!!");
28+
}
29+
30+
}
31+
```
32+
33+
Now whenever we do,
34+
35+
```java
36+
Animal animal = new Lion(); //creating instance of Animal, of type Lion
37+
animal.bark();
38+
```
39+
40+
Note: Initialising the `Animal` class with `Lion`.
41+
The output will look like
42+
43+
```java
44+
Lion here!!
45+
```
46+
47+
According to OOP, there are many types of inheritance, but Java supports only some of them(Multi-level and Hierarchical).
48+
To read more about it, please read [this][java-inheritance].
49+
50+
[java-inheritance]: https://www.javatpoint.com/inheritance-in-java#:~:text=On%20the%20basis%20of%20class,will%20learn%20about%20interfaces%20later.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "himanshugoyal1065",
5+
"exercism_username": "himanshugoyal1065"
6+
}
7+
],
8+
"forked_from": ["csharp/inheritance"]
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Goal
2+
3+
The goal of this exercise is to teach the student the basics of the Concept of `Inheritance` in Java.
4+
5+
## Learning objectives
6+
7+
- Know what inheritance is.
8+
- Know how to inherit from a class.
9+
- Know that all types inherit from object.
10+
11+
## Out of scope
12+
13+
- Inheritance from interfaces
14+
15+
## Concepts
16+
17+
- `inheritence`
18+
- `objects`
19+
20+
## Prerequisites
21+
22+
This exercise's prerequisites Concepts are:
23+
24+
- `classes`
25+
- `abstract`
26+
- `functions`
27+
- `strings`
28+
- `boolean`
29+
30+
## Representer
31+
32+
This exercise does not require any specific representation logic to be added to the [representer][representer-java].
33+
34+
## Analyzer
35+
36+
This exercise does not require any specific analyzer logic to be added to the [analyzer][analyzer-java].
37+
38+
[representer-java]: https://github.com/exercism/java-representer
39+
[analyzer-java]: https://github.com/exercism/java-analyzer
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
abstract class Fighter {
2+
3+
/**
4+
* this method sets the default vulnerability to false for all the child classes.
5+
*
6+
* @return the vulnerability i.e false.
7+
*/
8+
boolean isVulnerable() {
9+
return false;
10+
}
11+
12+
abstract int damagePoints(Fighter fighter);
13+
14+
}
15+
16+
class Warrior extends Fighter {
17+
18+
@Override
19+
public String toString() {
20+
return "Fighter is a Warrior";
21+
}
22+
23+
@Override
24+
int damagePoints(Fighter wizard) {
25+
if (wizard.isVulnerable()) {
26+
return 10;
27+
} else {
28+
return 6;
29+
}
30+
}
31+
}
32+
33+
class Wizard extends Fighter {
34+
35+
boolean isSpellPrepared = false;
36+
37+
@Override
38+
boolean isVulnerable() {
39+
if (isSpellPrepared == false) {
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
@Override
46+
int damagePoints(Fighter warrior) {
47+
if (isSpellPrepared) {
48+
return 12;
49+
} else {
50+
return 3;
51+
}
52+
}
53+
54+
void prepareSpell() {
55+
isSpellPrepared = true;
56+
}
57+
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.13"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
12+
}
13+
14+
test {
15+
testLogging {
16+
exceptionFormat = 'short'
17+
showStandardStreams = true
18+
events = ["passed", "failed", "skipped"]
19+
}
20+
}

0 commit comments

Comments
 (0)