File tree Expand file tree Collapse file tree 3 files changed +62
-58
lines changed
exercises/concept/inheritance/.docs Expand file tree Collapse file tree 3 files changed +62
-58
lines changed Original file line number Diff line number Diff line change 1- TODO: add information on inheritance concept
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/
Original file line number Diff line number Diff line change 1- []
1+ [
2+ {
3+ "url" : " https://www.geeksforgeeks.org/access-modifiers-java/" ,
4+ "description" : " access-modifiers"
5+ }
6+ ]
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments