Skip to content

Commit f12ce6d

Browse files
committed
improve: expand strings intro and rewrite inheritance concept docs
strings/introduction.md was only 13 lines with no examples of actual String methods. Expanded it to cover common methods, immutability, equals() vs ==, StringBuilder, and String.format. inheritance/introduction.md had a semantically wrong example (Lion.bark), awkward phrasing, a link to javatpoint instead of Oracle docs, and no mention of super or abstract classes. Full rewrite with a clean example and proper coverage. inheritance/about.md had invalid Java syntax: 'interface Animal()' — interfaces don't have parentheses. Fixed the interface declaration and updated the method name to match the introduction. Co-Authored-By: bull1210 <bull1210@gmail.com>
1 parent 1946e0f commit f12ce6d

3 files changed

Lines changed: 124 additions & 37 deletions

File tree

concepts/inheritance/about.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ These concepts are very similar and are often confused.
2727
- Inheritance means that the child has an IS-A relationship with the parent class.
2828

2929
```java
30-
interface Animal() {
31-
public void bark();
30+
interface Animal {
31+
void makeSound();
3232
}
3333

3434
class Dog implements Animal {
35-
public void bark() {
36-
System.out.println("Bark");
35+
public void makeSound() {
36+
System.out.println("Woof!");
3737
}
3838
}
3939
```
Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,88 @@
11
# Introduction
22

3-
Inheritance is a core concept in OOP (Object-Oriented Programming).
4-
It represents an IS-A relationship.
5-
It literally means in programming as it means in english, inheriting features from parent (in programming features is normally functions and variables).
6-
7-
Consider a class, `Animal` as shown,
3+
Inheritance lets one class build on top of another, reusing its fields and methods without rewriting them.
4+
The class being extended is called the **parent** (or superclass); the class doing the extending is the **child** (or subclass).
5+
The relationship is described as IS-A: a `Dog` IS-A `Animal`.
86

97
```java
10-
//Creating an Animal class with bark() as a member function.
118
public class Animal {
12-
13-
public void bark() {
14-
System.out.println("This is an animal");
9+
public void makeSound() {
10+
System.out.println("...");
1511
}
16-
1712
}
1813
```
1914

20-
`Animal` is a parent class, because the properties this class has can be extended to all the animals in general.
21-
22-
Consider an animal named `Lion`, having a class like,
15+
Use the `extends` keyword to inherit from a class:
2316

2417
```java
25-
//Lion class is a child class of Animal.
26-
public class Lion extends Animal {
18+
public class Dog extends Animal {
2719

2820
@Override
29-
public void bark() {
30-
System.out.println("Lion here!!");
21+
public void makeSound() {
22+
System.out.println("Woof!");
3123
}
32-
3324
}
3425
```
3526

3627
~~~~exercism/note
37-
The `Override` annotation is used to indicate that a method in a subclass is overriding a method of its superclass.
38-
It's not strictly necessary but it's a best practice to use it.
28+
The `@Override` annotation tells the compiler that you intend to replace a parent method.
29+
It's not required, but it's considered good practice — the compiler will warn you if the method name doesn't actually match anything in the parent.
3930
~~~~
4031

41-
Now whenever we do,
32+
Because `Dog` extends `Animal`, a `Dog` instance can be stored in an `Animal` variable.
33+
The actual type at runtime decides which method runs — this is called **polymorphism**:
4234

4335
```java
44-
Animal animal = new Lion(); //creating instance of Animal, of type Lion
45-
animal.bark();
36+
Animal animal = new Dog();
37+
animal.makeSound(); // prints "Woof!", not "..."
4638
```
4739

48-
Note: Initialising the `Animal` class with `Lion`.
49-
The output will look like
40+
## The `super` keyword
41+
42+
Inside a child class, `super` refers to the parent.
43+
You can use it to call the parent's constructor or a method you've overridden:
5044

5145
```java
52-
Lion here!!
46+
public class Dog extends Animal {
47+
private String name;
48+
49+
public Dog(String name) {
50+
super(); // calls Animal's constructor
51+
this.name = name;
52+
}
53+
54+
@Override
55+
public void makeSound() {
56+
super.makeSound(); // calls Animal's version first
57+
System.out.println(name + " says: Woof!");
58+
}
59+
}
60+
```
61+
62+
## Abstract classes
63+
64+
If it doesn't make sense to instantiate the parent directly, mark it `abstract`.
65+
An abstract class can declare methods without providing a body — each subclass is then required to implement them:
66+
67+
```java
68+
public abstract class Shape {
69+
public abstract double area(); // subclasses must implement this
70+
}
71+
72+
public class Circle extends Shape {
73+
private double radius;
74+
75+
public Circle(double radius) {
76+
this.radius = radius;
77+
}
78+
79+
@Override
80+
public double area() {
81+
return Math.PI * radius * radius;
82+
}
83+
}
5384
```
5485

55-
According to OOP, there are many types of inheritance, but Java supports only some of them (Multi-level and Hierarchical).
56-
To read more about it, please read [this][java-inheritance].
86+
To read more about inheritance in Java, see the [official documentation][java-inheritance].
5787

58-
[java-inheritance]: https://www.javatpoint.com/inheritance-in-java#:~:text=On%20the%20basis%20of%20class,will%20learn%20about%20interfaces%20later.
88+
[java-inheritance]: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

concepts/strings/introduction.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,64 @@ Double quotes are used to define a `String` instance:
77
String fruit = "Apple";
88
```
99

10-
Strings are manipulated by calling the string's methods.
11-
Once a string has been constructed, its value can never change.
12-
Any methods that appear to modify a string will actually return a new string.
13-
The `String` class provides some _static_ methods to transform the strings.
10+
## Common string operations
11+
12+
Strings expose a rich set of methods. Here are the ones you'll reach for most often:
13+
14+
```java
15+
String s = "Hello, World!";
16+
17+
s.length(); // 13
18+
s.toLowerCase(); // "hello, world!"
19+
s.toUpperCase(); // "HELLO, WORLD!"
20+
s.trim(); // removes leading/trailing whitespace
21+
s.contains("World"); // true
22+
s.startsWith("Hello"); // true
23+
s.endsWith("!"); // true
24+
s.indexOf("World"); // 7
25+
s.substring(7, 12); // "World"
26+
s.replace("World", "Java"); // "Hello, Java!"
27+
s.split(", "); // ["Hello", "World!"]
28+
```
29+
30+
## Strings are immutable
31+
32+
Once a string is created, its value never changes.
33+
Any method that appears to modify a string actually returns a new one — the original is untouched:
34+
35+
```java
36+
String original = "hello";
37+
String upper = original.toUpperCase();
38+
// original is still "hello", upper is "HELLO"
39+
```
40+
41+
## Comparing strings
42+
43+
Use `.equals()` to compare string values — never `==`, which only checks if two variables point to the same object:
44+
45+
```java
46+
String a = "hello";
47+
String b = "hello";
48+
a.equals(b); // true (correct)
49+
a.equalsIgnoreCase(b); // true (case-insensitive version)
50+
```
51+
52+
## Building strings dynamically
53+
54+
When you need to construct a string piece by piece (e.g. inside a loop), use `StringBuilder` instead of concatenating with `+`.
55+
Each `+` on a `String` creates a new object, which gets expensive fast:
56+
57+
```java
58+
StringBuilder sb = new StringBuilder();
59+
sb.append("Hello");
60+
sb.append(", ");
61+
sb.append("World!");
62+
String result = sb.toString(); // "Hello, World!"
63+
```
64+
65+
For simple one-liner formatting, `String.format` is handy:
66+
67+
```java
68+
String.format("Hello, %s! You are %d years old.", "Alice", 30);
69+
// "Hello, Alice! You are 30 years old."
70+
```

0 commit comments

Comments
 (0)