[JAVA-621] core-java-lang-oop-others module

* Creation

* Moved code from www.baeldung.com/java-static-dynamic-binding

* Recreated code for https://www.baeldung.com/java-oop as none was
matching anymore

* Moved code from https://www.baeldung.com/java-pass-by-value-or-pass-by-reference

* Moved article references to the new README.md
This commit is contained in:
dupirefr 2020-04-04 12:05:22 +02:00
parent b1e507d44c
commit f6d3dc711f
20 changed files with 277 additions and 11 deletions

View File

@ -0,0 +1,8 @@
## Core Java Lang OOP - Others
This module contains articles about Object Oriented Programming (OOP) in Java
### Relevant Articles:
- [Object-Oriented-Programming Concepts in Java](https://www.baeldung.com/java-oop)
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
- [Pass-By-Value as a Parameter Passing Mechanism in Java](https://www.baeldung.com/java-pass-by-value-or-pass-by-reference)

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>core-java-lang-oop-modules</artifactId>
<groupId>com.baeldung.core-java-lang-oop-modules</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lang-oop-others</artifactId>
<name>core-java-lang-oop-others</name>
<packaging>jar</packaging>
</project>

View File

@ -15,7 +15,7 @@ public class AnimalActivity {
logger.info("Animal is sleeping"); logger.info("Animal is sleeping");
} }
public static void sleep(Cat cat) { public static void sleep(Dog dog) {
logger.info("Cat is sleeping"); logger.info("Cat is sleeping");
} }
@ -30,7 +30,7 @@ public class AnimalActivity {
//assigning a dog object to reference of type Animal //assigning a dog object to reference of type Animal
Animal catAnimal = new Cat(); Animal catAnimal = new Dog();
catAnimal.makeNoise(); catAnimal.makeNoise();

View File

@ -6,9 +6,9 @@ import org.slf4j.LoggerFactory;
/** /**
* Created by madhumita.g on 25-07-2018. * Created by madhumita.g on 25-07-2018.
*/ */
public class Cat extends Animal { public class Dog extends Animal {
final static Logger logger = LoggerFactory.getLogger(Cat.class); final static Logger logger = LoggerFactory.getLogger(Dog.class);
public void makeNoise() { public void makeNoise() {

View File

@ -0,0 +1,13 @@
package com.baeldung.oop;
public class ArmoredCar extends Car {
private int bulletProofWindows;
public ArmoredCar(String type, String model, String color) {
super(type, model, color);
}
public void remoteStartCar() {
// this vehicle can be started by using a remote control
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.oop;
public class Car extends Vehicle {
private String type;
private String color;
private int speed;
private int numberOfGears;
public Car(String type, String model, String color) {
super(4, model);
this.type = type;
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int increaseSpeed(int increment) {
if (increment > 0) {
this.speed += increment;
} else {
System.out.println("Increment can't be negative.");
}
return this.speed;
}
public int decreaseSpeed(int decrement) {
if (decrement > 0 && decrement <= this.speed) {
this.speed -= decrement;
} else {
System.out.println("Decrement can't be negative or greater than current speed.");
}
return this.speed;
}
public void openDoors() {
// process to open the doors
}
@Override
public void honk() {
// produces car-specific honk
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.oop;
import java.util.Date;
public class GenericFile {
private String name;
private String extension;
private Date dateCreated;
private String version;
private byte[] content;
public GenericFile() {
this.setDateCreated(new Date());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getFileInfo() {
return "Generic File Impl";
}
public Object read() {
return content;
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.oop;
public class ImageFile extends GenericFile {
private int height;
private int width;
public ImageFile(String name, int height, int width, byte[] content, String version) {
this.setHeight(height);
this.setWidth(width);
this.setContent(content);
this.setName(name);
this.setVersion(version);
this.setExtension(".jpg");
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getFileInfo() {
return "Image File Impl";
}
public String read() {
return this.getContent()
.toString();
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.oop;
public class TextFile extends GenericFile {
private int wordCount;
public TextFile(String name, String content, String version) {
String[] words = content.split(" ");
this.setWordCount(words.length > 0 ? words.length : 1);
this.setContent(content.getBytes());
this.setName(name);
this.setVersion(version);
this.setExtension(".txt");
}
public int getWordCount() {
return wordCount;
}
public void setWordCount(int wordCount) {
this.wordCount = wordCount;
}
public String getFileInfo() {
return "Text File Impl";
}
public String read() {
return this.getContent()
.toString();
}
public String read(int limit) {
return this.getContent()
.toString()
.substring(0, limit);
}
public String read(int start, int stop) {
return this.getContent()
.toString()
.substring(start, stop);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.oop;
public class Vehicle {
private int wheels;
private String model;
public Vehicle(int wheels, String model) {
this.wheels = wheels;
this.model = model;
}
public void start() {
// the process of starting the vehicle
}
public void stop() {
// process to stop the vehicle
}
public void honk() {
// produces a default honk
}
}

View File

@ -62,9 +62,9 @@ public class AnimalActivityUnitTest {
@Test @Test
public void givenDogReference__whenRefersCatObject_shouldCallFunctionWithAnimalParam() { public void givenDogReference__whenRefersCatObject_shouldCallFunctionWithAnimalParam() {
Cat cat = new Cat(); Dog dog = new Dog();
AnimalActivity.sleep(cat); AnimalActivity.sleep(dog);
verify(mockAppender).doAppend(captorLoggingEvent.capture()); verify(mockAppender).doAppend(captorLoggingEvent.capture());
@ -79,7 +79,7 @@ public class AnimalActivityUnitTest {
@Test @Test
public void givenAnimaReference__whenRefersDogObject_shouldCallFunctionWithAnimalParam() { public void givenAnimaReference__whenRefersDogObject_shouldCallFunctionWithAnimalParam() {
Animal cat = new Cat(); Animal cat = new Dog();
AnimalActivity.sleep(cat); AnimalActivity.sleep(cat);

View File

@ -22,7 +22,7 @@ import static org.mockito.Mockito.verify;
* Created by madhumita.g on 01-08-2018. * Created by madhumita.g on 01-08-2018.
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CatUnitTest { public class DogUnitTest {
@Mock @Mock
private Appender mockAppender; private Appender mockAppender;
@ -45,9 +45,9 @@ public class CatUnitTest {
@Test @Test
public void makeNoiseTest() { public void makeNoiseTest() {
Cat cat = new Cat(); Dog dog = new Dog();
cat.makeNoise(); dog.makeNoise();
verify(mockAppender).doAppend(captorLoggingEvent.capture()); verify(mockAppender).doAppend(captorLoggingEvent.capture());

View File

@ -22,5 +22,6 @@
<module>core-java-lang-oop-types</module> <module>core-java-lang-oop-types</module>
<module>core-java-lang-oop-inheritance</module> <module>core-java-lang-oop-inheritance</module>
<module>core-java-lang-oop-methods</module> <module>core-java-lang-oop-methods</module>
<module>core-java-lang-oop-others</module>
</modules> </modules>
</project> </project>

View File

@ -5,5 +5,4 @@ This module contains articles about Object-oriented programming (OOP) in Java
### Relevant Articles: ### Relevant Articles:
- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy) - [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy)
- [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure) - [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure)
- [Object-Oriented-Programming Concepts in Java](https://www.baeldung.com/java-oop)
- [[More -->]](/core-java-modules/core-java-lang-oop-2) - [[More -->]](/core-java-modules/core-java-lang-oop-2)