Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d9c4252

Browse files
authoredFeb 8, 2022
Inheritance in Java
1 parent 7bc48ba commit d9c4252

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
 

‎45.Inheritance/README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Inheritance in Java
2+
- You might have heard people saying your nose is similar to your father or mother. Or, more formally, we can say that you've inherited the genes from your parents due to which you look similar to them.
3+
- The same phenomenon of inheritance is also valid in programming.
4+
- In Java, one class can easily inherit the attributes and methods from some other class. This mechanism of acquiring objects and properties from some other class is known as inheritance in Java.
5+
- Inheritance is used to borrow properties & methods from an existing class.
6+
- Inheritance helps us create classes based on existing classes, which increases the code's reusability.
7+
- Examples :
8+
9+
<img src="https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/base64.png" alt="">
10+
11+
<img src="https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/base64_LPzvgK0.png" alt="">
12+
13+
### Important terminologies used in Inheritance :
14+
- Parent class/superclass: The class from which a class inherits methods and attributes is known as parent class.
15+
- Child class/sub-class: The class that inherits some other class's methods and attributes is known as child class.
16+
17+
<img src="https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/base64_2dSyfKW.png" alt="">
18+
19+
### Extends keyword in inheritance :
20+
- The extends keyword is used to inherit a subclass from a superclass.
21+
- Syntax :
22+
23+
```
24+
class Subclass-name extends Superclass-name
25+
{
26+
//methods and fields
27+
}
28+
```
29+
30+
- Example :
31+
32+
```
33+
public class dog extends Animal {
34+
// code
35+
}
36+
```
37+
38+
**Note:** [Java doesn't support multiple inheritances](https://codewithharry.com/videos/java-tutorials-for-beginners-56), i.e., two classes cannot be the superclass for a subclass.
39+
40+
**Quick quiz:** Create a class Animal and Derive another class dog from it.
41+
42+
```
43+
class Animal{
44+
public void printMe(){
45+
System.out.println("I am a Animal Class.");
46+
}
47+
}
48+
49+
class Derived extends Dog{
50+
public void bark() {
51+
System.out.println("barking...");
52+
}
53+
}
54+
55+
public class cwh_45_inheritance {
56+
public static void main(String[] args) {
57+
}
58+
}
59+
```
60+
61+
### Code as described in video
62+
63+
```
64+
class Base{
65+
public int x;
66+
67+
public int getX() {
68+
return x;
69+
}
70+
71+
public void setX(int x) {
72+
System.out.println("I am in base and setting x now");
73+
this.x = x;
74+
}
75+
76+
public void printMe(){
77+
System.out.println("I am a constructor");
78+
}
79+
}
80+
81+
class Derived extends Base{
82+
public int y;
83+
84+
public int getY() {
85+
return y;
86+
}
87+
88+
public void setY(int y) {
89+
this.y = y;
90+
}
91+
}
92+
93+
public class cwh_45_inheritance {
94+
public static void main(String[] args) {
95+
// Creating an Object of base class
96+
Base b = new Base();
97+
b.setX(4);
98+
System.out.println(b.getX());
99+
100+
// Creating an object of derived class
101+
Derived d = new Derived();
102+
d.setY(43);
103+
System.out.println(d.getY());
104+
}
105+
}
106+
```
107+
108+
**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/Chapter10.pdf)**
109+
110+
**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-45/UltimateJavaCheatSheet.pdf)**

0 commit comments

Comments
 (0)
Please sign in to comment.