File tree Expand file tree Collapse file tree
objectorientedprogramming/inheritance Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * Book class used to explain the encapsulation.
3+ *
4+ * @version 1.0
5+ * @author L.Gobinath
6+ */
7+ public class Book {
8+ // Title of the book.
9+ private String title ;
10+ // Author of the book.
11+ private String author ;
12+ // Price of the book.
13+ private double price ;
14+
15+ /**
16+ * Get the title.
17+ * @return The title of the book.
18+ */
19+ public String getTitle () {
20+ return this .title ;
21+ }
22+
23+ /**
24+ * Set the title.
25+ * @param title The title of the book.
26+ */
27+ public void setTitle (String title ) {
28+ this .title = title ;
29+ }
30+
31+ /**
32+ * Get the author of the book.
33+ * @return The author of the book.
34+ */
35+ public String getAuthor () {
36+ return this .author ;
37+ }
38+
39+ /**
40+ * Set the author of the book.
41+ * @param author The author of the book.
42+ */
43+ public void setAuthor (String author ) {
44+ this .author = author ;
45+ }
46+
47+ /**
48+ * Set the price.
49+ * @param price Price of the book.
50+ */
51+ public void setPrice (double price ) {
52+ if (price > 0 ) {
53+ this .price = price ;
54+ } else {
55+ System .out .println ("Invalid price" );
56+ }
57+ }
58+
59+ /**
60+ * Get the price.
61+ * @return Price of the book.
62+ */
63+ public double getPrice () {
64+ return this .price ;
65+ }
66+
67+ /**
68+ * Print the total amount for given quantity.
69+ * @param qty The quantity
70+ */
71+ public void sell (int qty ) {
72+ System .out .println ("Total: $" + (qty * price ));
73+ }
74+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Testing class for Book class.
3+ *
4+ * @version 1.0
5+ * @author L.Gobinath
6+ */
7+ public class BookShop {
8+ public static void main (String [] args ) {
9+ Book book = new Book ();
10+ book .setTitle ("Hamlet" );
11+ book .setAuthor ("William Shakespeare" );
12+ book .setPrice (5.95 );
13+
14+ Magazine magazine = new Magazine ();
15+ magazine .setTitle ("TIME" );
16+ magazine .setIssue ("Feb. 2, 2015" );
17+ magazine .setPrice (0.58 );
18+
19+ book .sell (2 );
20+ magazine .sell (52 );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Magazine class used to explain the inheritance.
3+ *
4+ * @version 1.0
5+ * @author L.Gobinath
6+ */
7+ public class Magazine {
8+ // Title of the magazine.
9+ private String title ;
10+ // Author of the magazine.
11+ private String issue ;
12+ // Price of the magazine.
13+ private double price ;
14+
15+ /**
16+ * Get the title.
17+ * @return The title of the magazine.
18+ */
19+ public String getTitle () {
20+ return this .title ;
21+ }
22+
23+ /**
24+ * Set the title.
25+ * @param title The title of the magazine.
26+ */
27+ public void setTitle (String title ) {
28+ this .title = title ;
29+ }
30+
31+ /**
32+ * Get the issue of the magazine.
33+ * @return The issue of the magazine.
34+ */
35+ public String getIssue () {
36+ return this .issue ;
37+ }
38+
39+ /**
40+ * Set the issue of the magazine.
41+ * @param issue The issue of the magazine.
42+ */
43+ public void setIssue (String issue ) {
44+ this .issue = issue ;
45+ }
46+
47+ /**
48+ * Set the price.
49+ * @param price Price of the magazine.
50+ */
51+ public void setPrice (double price ) {
52+ if (price > 0 ) {
53+ this .price = price ;
54+ } else {
55+ System .out .println ("Invalid price" );
56+ }
57+ }
58+
59+ /**
60+ * Get the price.
61+ * @return Price of the magazine.
62+ */
63+ public double getPrice () {
64+ return this .price ;
65+ }
66+
67+ /**
68+ * Print the total amount for given quantity.
69+ * @param qty The quantity
70+ */
71+ public void sell (int qty ) {
72+ System .out .println ("Total: $" + (qty * price ));
73+ }
74+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Book class used to explain the encapsulation.
3+ * Book is a sub class of Publication.
4+ *
5+ * @version 2.0
6+ * @author L.Gobinath
7+ */
8+ public class Book extends Publication {
9+ // Author of the book.
10+ private String author ;
11+
12+ /**
13+ * Get the author of the book.
14+ * @return The author of the book.
15+ */
16+ public String getAuthor () {
17+ return this .author ;
18+ }
19+
20+ /**
21+ * Set the author of the book.
22+ * @param author The author of the book.
23+ */
24+ public void setAuthor (String author ) {
25+ this .author = author ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Testing class for Book class.
3+ * There are no changes in this class.
4+ * @version 2.0
5+ * @author L.Gobinath
6+ */
7+ public class BookShop {
8+ public static void main (String [] args ) {
9+ Book book = new Book ();
10+ book .setTitle ("Hamlet" );
11+ book .setAuthor ("William Shakespeare" );
12+ book .setPrice (5.95 );
13+
14+ Magazine magazine = new Magazine ();
15+ magazine .setTitle ("TIME" );
16+ magazine .setIssue ("Feb. 2, 2015" );
17+ magazine .setPrice (0.58 );
18+
19+ book .sell (2 );
20+ magazine .sell (52 );
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Magazine class used to explain the inheritance.
3+ * Magazine is a sub class of Publication.
4+ *
5+ * @version 2.0
6+ * @author L.Gobinath
7+ */
8+ public class Magazine extends Publication {
9+ // Author of the magazine.
10+ private String issue ;
11+
12+ /**
13+ * Get the issue of the magazine.
14+ * @return The issue of the magazine.
15+ */
16+ public String getIssue () {
17+ return this .issue ;
18+ }
19+
20+ /**
21+ * Set the issue of the magazine.
22+ * @param issue The issue of the magazine.
23+ */
24+ public void setIssue (String issue ) {
25+ this .issue = issue ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Publication super class used to explain the inheritance.
3+ *
4+ * @version 2.0
5+ * @author L.Gobinath
6+ */
7+ public class Publication {
8+ // Title of the publication.
9+ private String title ;
10+ // Price of the publication.
11+ private double price ;
12+
13+ /**
14+ * Get the title.
15+ * @return The title of the publication.
16+ */
17+ public String getTitle () {
18+ return this .title ;
19+ }
20+
21+ /**
22+ * Set the title.
23+ * @param title The title of the publication.
24+ */
25+ public void setTitle (String title ) {
26+ this .title = title ;
27+ }
28+
29+ /**
30+ * Set the price.
31+ * @param price Price of the publication.
32+ */
33+ public void setPrice (double price ) {
34+ if (price > 0 ) {
35+ this .price = price ;
36+ } else {
37+ System .out .println ("Invalid price" );
38+ }
39+ }
40+
41+ /**
42+ * Get the price.
43+ * @return Price of the publication.
44+ */
45+ public double getPrice () {
46+ return this .price ;
47+ }
48+
49+ /**
50+ * Print the total amount for given quantity.
51+ * @param qty The quantity
52+ */
53+ public void sell (int qty ) {
54+ System .out .println ("Total: $" + (qty * price ));
55+ }
56+ }
You can’t perform that action at this time.
0 commit comments