Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Compiled class file
.idea/*
*.class

# Log file
*.log

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# DesignPatternsJava9
# Video 1.8: Issues faced due to force fitting everything in object inheritance
In the branch, inheritance is expalined with its issues. Needs Java 9 and mongodb setup to run the example.

# Definition of inheritance
Inheritance is a mechanism in which one object acquires all the properties and behaviours of the parent object. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation to maintain the same behaviour to reuse code.

# Issues with inheritance
The main problem with implementation inheritance is that it introduces unnecessary coupling in the form of the "fragile base class problem" modifications to the base class implementation can cause inadvertent behavioral changes in subclasses, creates strong coupling and loose flexibility.

## DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.

11 changes: 11 additions & 0 deletions com.premaseem.inheritance/com.premaseem.inheritance.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/

package com.premaseem.inheritance;

import com.premaseem.inheritance.model.Developer;
import com.premaseem.inheritance.model.Hr;
import com.premaseem.inheritance.model.QE;


public class Client {
public static void main (String[] args) {
Developer employee1 = new Developer("Aseem", 20,100);
QE employee2 = new QE("Aseem", 20,100);
Hr employee3 = new Hr("Sony",1000,10);

System.out.println("Inheritance concept");
System.out.println(employee1);
System.out.println(employee2);
System.out.println(employee3);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.premaseem.inheritance.model;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Developer extends Employee{

public Developer(String name, Integer workHours, Integer rate) {
super(name, rate, workHours,null,null);
}

@Override
public String toString() {
return "Developer{" +
"name='" + getName() + '\'' +
", salary=" + getSalary() +
", workHours=" + getWorkHours() +
", rate=" + getRate() +
'}';
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.premaseem.inheritance.model;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Employee {

protected String name;
protected Integer workHours;
protected Integer rate;
private Float salary;
protected Integer baseSalary;
protected Integer bonusPercent;

public Employee (String name, Integer rate, Integer workHours,Integer baseSalary,Integer bonusPercent) {
this.name = name;
this.rate = rate;
this.workHours = workHours;
this.baseSalary = baseSalary;
this.bonusPercent = bonusPercent;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public Float getSalary() {
if (this.getClass().getName().equalsIgnoreCase("com.premaseem.inheritance.model.Hr") ){
salary = (float) baseSalary + bonusPercent;
}else{
salary = (float) workHours * rate;
}
return salary;
}

public Integer getWorkHours() {
return workHours;
}

public void setWorkHours(Integer workHours) {
this.workHours = workHours;
}

public Integer getRate() {
return rate;
}

public void setRate(Integer rate) {
this.rate = rate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.premaseem.inheritance.model;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Hr extends Employee{

public Hr (String name, Integer baseSalary, Integer bonusPercent) {
super(name,null,null, baseSalary, bonusPercent);
}

@Override
public String toString() {
return "Hr{" +
"name='" + getName() + '\'' +
", salary=" + getSalary() +
'}';
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.premaseem.inheritance.model;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class QE extends Employee{

public QE (String name, Integer workHours, Integer rate) {
super(name, rate, workHours,null,null);
}

@Override
public String toString() {
return "Developer{" +
"name='" + getName() + '\'' +
", salary=" + getSalary() +
", workHours=" + getWorkHours() +
", rate=" + getRate() +
'}';
}


}
9 changes: 9 additions & 0 deletions com.premaseem.inheritance/src/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
module com.premaseem.inheritance {

}