├── .gitignore ├── README.md └── src └── com └── in28minutes └── java └── oops ├── cohesion ├── problem │ └── CohesionExampleProblem.java └── solution │ └── CohesionExampleSolution.java ├── coupling ├── problem │ └── CouplingExamplesProblem.java └── solution │ └── CouplingExamplesSolution.java ├── encapsulation └── EncapsulationExample.java ├── inheritance └── InheritanceExamples.java └── interfaces └── InterfaceExamples.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | *.cmd 21 | *.classpath 22 | *.settings 23 | *.project 24 | *.mvn 25 | mvnw 26 | target 27 | *.DS_Store 28 | 29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 30 | hs_err_pid* 31 | bin 32 | .settings 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaObjectOrientedProgramming 2 | 3 | Learn basics of object oriented programming with Java 4 | 5 | Learn about these concepts with Java Examples 6 | - Inheritance 7 | - Interfaces 8 | - Polymorphism 9 | - Abstract class 10 | - Coupling 11 | - Cohesion 12 | - Encapsulation 13 | 14 | ### Troubleshooting 15 | - Refer our TroubleShooting Guide - https://github.com/in28minutes/in28minutes-initiatives/tree/master/The-in28Minutes-TroubleshootingGuide-And-FAQ 16 | 17 | ## Youtube Playlists - 500+ Videos 18 | 19 | [Click here - 30+ Playlists with 500+ Videos on Spring, Spring Boot, REST, Microservices and the Cloud](https://www.youtube.com/user/rithustutorials/playlists?view=1&sort=lad&flow=list) 20 | 21 | ## Keep Learning in28Minutes 22 | 23 | in28Minutes is creating amazing solutions for you to learn Spring Boot, Full Stack and the Cloud - Docker, Kubernetes, AWS, React, Angular etc. - [Check out all our courses here](https://github.com/in28minutes/learn) 24 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/cohesion/problem/CohesionExampleProblem.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.cohesion.problem; 2 | 3 | //Cohesion is a measure of how related the responsibilities of a class are. 4 | //This class is downloading from internet, parsing data and storing data to database. 5 | //The responsibilities of this class are not really related. This is not cohesive class. 6 | class DownloadAndStore { 7 | void downloadFromInternet() { 8 | 9 | } 10 | 11 | void parseData() { 12 | 13 | } 14 | 15 | void storeIntoDatabase() { 16 | 17 | } 18 | 19 | void doEverything() { 20 | downloadFromInternet(); 21 | parseData(); 22 | storeIntoDatabase(); 23 | } 24 | } 25 | 26 | public class CohesionExampleProblem { 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/cohesion/solution/CohesionExampleSolution.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.cohesion.solution; 2 | 3 | //This is a better way of approaching the problem. Different classes have their 4 | //own responsibilities. 5 | 6 | class InternetDownloader { 7 | public void downloadFromInternet() { 8 | 9 | } 10 | } 11 | 12 | class DataParser { 13 | public void parseData(String content) { 14 | } 15 | } 16 | 17 | class DatabaseStorer { 18 | public void storeIntoDatabase(String data) { 19 | } 20 | } 21 | 22 | class DownloadAndStore { 23 | void doEverything() { 24 | new InternetDownloader().downloadFromInternet(); 25 | new DataParser().parseData(""); 26 | new DatabaseStorer().storeIntoDatabase(""); 27 | } 28 | } 29 | 30 | public class CohesionExampleSolution { 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/coupling/problem/CouplingExamplesProblem.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.coupling.problem; 2 | 3 | //Coupling is a measure of how much a class is dependent on other classes. 4 | //There should minimal dependencies between classes. 5 | //Consider the example below: 6 | 7 | class ShoppingCartEntry { 8 | public float price; 9 | public int quantity; 10 | } 11 | 12 | class ShoppingCart { 13 | public ShoppingCartEntry[] items; 14 | } 15 | 16 | class Order { 17 | private ShoppingCart cart; 18 | private float salesTax; 19 | 20 | public Order(ShoppingCart cart, float salesTax) { 21 | this.cart = cart; 22 | this.salesTax = salesTax; 23 | } 24 | 25 | // This method know the internal details of ShoppingCartEntry and 26 | // ShoppingCart classes. If there is any change in any of those 27 | // classes, this method also needs to change. 28 | public float orderTotalPrice() { 29 | float cartTotalPrice = 0; 30 | for (int i = 0; i < cart.items.length; i++) { 31 | cartTotalPrice += cart.items[i].price * cart.items[i].quantity; 32 | } 33 | cartTotalPrice += cartTotalPrice * salesTax; 34 | return cartTotalPrice; 35 | } 36 | } 37 | 38 | public class CouplingExamplesProblem { 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/coupling/solution/CouplingExamplesSolution.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.coupling.solution; 2 | 3 | class ShoppingCartEntry { 4 | float pricedummy; 5 | int quantity; 6 | 7 | public float getTotalPrice() { 8 | return pricedummy * quantity; 9 | } 10 | } 11 | 12 | class CartContents { 13 | ShoppingCartEntry[] items; 14 | 15 | public float getTotalPrice() { 16 | float totalPrice = 0; 17 | for (ShoppingCartEntry item : items) { 18 | totalPrice += item.getTotalPrice(); 19 | } 20 | return totalPrice; 21 | } 22 | } 23 | 24 | class Order { 25 | private CartContents cart; 26 | private float salesTax; 27 | 28 | public Order(CartContents cart, float salesTax) { 29 | this.cart = cart; 30 | this.salesTax = salesTax; 31 | } 32 | 33 | public float totalPrice() { 34 | return cart.getTotalPrice() * (1.0f + salesTax); 35 | } 36 | } 37 | 38 | public class CouplingExamplesSolution { 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/encapsulation/EncapsulationExample.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.encapsulation; 2 | 3 | class CricketScorer { 4 | private int score = 0; 5 | 6 | public int getScore() { 7 | return score; 8 | } 9 | 10 | public void addRuns(int score) { 11 | this.score = this.score + score; 12 | } 13 | 14 | public void six() { 15 | addRuns(6); 16 | } 17 | 18 | public void four() { 19 | addRuns(4); 20 | } 21 | 22 | public void single() { 23 | addRuns(1); 24 | } 25 | 26 | public void printScore() { 27 | System.out.println("Score : " + score); 28 | } 29 | 30 | } 31 | 32 | public class EncapsulationExample { 33 | public static void main(String[] args) { 34 | CricketScorer scorer = new CricketScorer(); 35 | scorer.four(); 36 | scorer.four(); 37 | scorer.single(); 38 | scorer.six(); 39 | scorer.six(); 40 | scorer.six(); 41 | scorer.printScore(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/inheritance/InheritanceExamples.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.inheritance; 2 | 3 | abstract class Animal { 4 | String name; 5 | 6 | //cool functionality 7 | abstract String bark(); 8 | } 9 | 10 | class Dog extends Animal { 11 | String bark() { 12 | return "Bow Bow"; 13 | } 14 | } 15 | 16 | class Cat extends Animal { 17 | String bark() { 18 | return "Meow Meow"; 19 | } 20 | } 21 | 22 | public class InheritanceExamples { 23 | public static void main(String[] args) { 24 | Animal animal = new Cat(); 25 | System.out.println(animal.bark()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/in28minutes/java/oops/interfaces/InterfaceExamples.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.java.oops.interfaces; 2 | 3 | interface Flyable { 4 | void fly(); 5 | } 6 | 7 | class Aeroplane implements Flyable { 8 | public void fly() { 9 | System.out.println("Aeroplane is flying"); 10 | } 11 | } 12 | 13 | class Bird implements Flyable { 14 | public void fly() { 15 | System.out.println("Bird is flying"); 16 | } 17 | } 18 | 19 | public class InterfaceExamples { 20 | public static void main(String[] args) { 21 | Flyable flyable = new Bird(); 22 | flyable.fly(); 23 | } 24 | } 25 | --------------------------------------------------------------------------------