[JAVA-621] core-java-lang-oop-inheritance module
* Creation * Moved code from https://www.baeldung.com/java-anonymous-classes * Moved code from www.baeldung.com/java-polymorphism * Moved code from www.baeldung.com/java-inheritance * Moved code from www.baeldung.com/java-variable-method-hiding * Moved code from https://www.baeldung.com/java-type-casting * Moved code from https://www.baeldung.com/java-super * Moved code from www.baeldung.com/java-interfaces * Moved code from www.baeldung.com/java-abstract-class * Moved code from www.baeldung.com/java-inner-interfaces * Moved article references to the new README.md
This commit is contained in:
@@ -4,12 +4,8 @@ This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to hashCode() in Java](https://www.baeldung.com/java-hashcode)
|
||||
- [Polymorphism in Java](https://www.baeldung.com/java-polymorphism)
|
||||
- [Method Overloading and Overriding in Java](https://www.baeldung.com/java-method-overload-override)
|
||||
- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy)
|
||||
- [Guide to Inheritance in Java](https://www.baeldung.com/java-inheritance)
|
||||
- [Object Type Casting in Java](https://www.baeldung.com/java-type-casting)
|
||||
- [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure)
|
||||
- [Variable and Method Hiding in Java](https://www.baeldung.com/java-variable-method-hiding)
|
||||
- [Object-Oriented-Programming Concepts in Java](https://www.baeldung.com/java-oop)
|
||||
- [[More -->]](/core-java-modules/core-java-lang-oop-2)
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Animal {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Animal.class);
|
||||
|
||||
public void eat() {
|
||||
LOGGER.info("animal is eating");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AnimalFeeder {
|
||||
|
||||
public void feed(List<Animal> animals) {
|
||||
animals.forEach(animal -> {
|
||||
animal.eat();
|
||||
if (animal instanceof Cat) {
|
||||
((Cat) animal).meow();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void uncheckedFeed(List<Animal> animals) {
|
||||
animals.forEach(animal -> {
|
||||
animal.eat();
|
||||
((Cat) animal).meow();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AnimalFeederGeneric<T> {
|
||||
private Class<T> type;
|
||||
|
||||
public AnimalFeederGeneric(Class<T> type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<T> feed(List<Animal> animals) {
|
||||
List<T> list = new ArrayList<T>();
|
||||
animals.forEach(animal -> {
|
||||
if (type.isInstance(animal)) {
|
||||
T objAsType = type.cast(animal);
|
||||
list.add(objAsType);
|
||||
}
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Cat extends Animal implements Mew {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Cat.class);
|
||||
|
||||
public void eat() {
|
||||
LOGGER.info("cat is eating");
|
||||
}
|
||||
|
||||
public void meow() {
|
||||
LOGGER.info("meow");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Dog extends Animal {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Dog.class);
|
||||
|
||||
public void eat() {
|
||||
LOGGER.info("dog is eating");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
public interface Mew {
|
||||
public void meow();
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public class ArmoredCar extends Car implements Floatable, Flyable{
|
||||
private int bulletProofWindows;
|
||||
private String model;
|
||||
|
||||
public void remoteStartCar() {
|
||||
// this vehicle can be started by using a remote control
|
||||
}
|
||||
|
||||
public String registerModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public String getAValue() {
|
||||
return super.model; // returns value of model defined in base class Car
|
||||
// return this.model; // will return value of model defined in ArmoredCar
|
||||
// return model; // will return value of model defined in ArmoredCar
|
||||
}
|
||||
|
||||
public static String msg() {
|
||||
// return super.msg(); // this won't compile.
|
||||
return "ArmoredCar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void floatOnWater() {
|
||||
System.out.println("I can float!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fly() {
|
||||
System.out.println("I can fly!");
|
||||
}
|
||||
|
||||
public void aMethod() {
|
||||
// System.out.println(duration); // Won't compile
|
||||
System.out.println(Floatable.duration); // outputs 10
|
||||
System.out.println(Flyable.duration); // outputs 20
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public class BMW extends Car {
|
||||
public BMW() {
|
||||
super(5, "BMW");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public class Car {
|
||||
private final int DEFAULT_WHEEL_COUNT = 5;
|
||||
private final String DEFAULT_MODEL = "Basic";
|
||||
|
||||
protected int wheels;
|
||||
protected String model;
|
||||
|
||||
public Car() {
|
||||
this.wheels = DEFAULT_WHEEL_COUNT;
|
||||
this.model = DEFAULT_MODEL;
|
||||
}
|
||||
|
||||
public Car(int wheels, String model) {
|
||||
this.wheels = wheels;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
// Check essential parts
|
||||
// If okay, start.
|
||||
}
|
||||
public static int count = 10;
|
||||
public static String msg() {
|
||||
return "Car";
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public class Employee {
|
||||
private String name;
|
||||
private Car car;
|
||||
|
||||
public Employee(String name, Car car) {
|
||||
this.name = name;
|
||||
this.car = car;
|
||||
}
|
||||
|
||||
public Car getCar() {
|
||||
return car;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public interface Floatable {
|
||||
int duration = 10;
|
||||
void floatOnWater();
|
||||
|
||||
default void repair() {
|
||||
System.out.println("Repairing Floatable object");
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public interface Flyable {
|
||||
int duration = 10;
|
||||
void fly();
|
||||
|
||||
/*
|
||||
* Commented
|
||||
*/
|
||||
//default void repair() {
|
||||
// System.out.println("Repairing Flyable object");
|
||||
//}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public class SpaceCar extends Car implements SpaceTraveller {
|
||||
@Override
|
||||
public void floatOnWater() {
|
||||
System.out.println("SpaceCar floating!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fly() {
|
||||
System.out.println("SpaceCar flying!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remoteControl() {
|
||||
System.out.println("SpaceCar being controlled remotely!");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
public interface SpaceTraveller extends Floatable, Flyable {
|
||||
int duration = 10;
|
||||
void remoteControl();
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FileManager {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(FileManager.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericFile file1 = new TextFile("SampleTextFile", "This is a sample text content", "v1.0.0");
|
||||
logger.info("File Info: \n" + file1.getFileInfo() + "\n");
|
||||
ImageFile imageFile = new ImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString()
|
||||
.getBytes(), "v1.0.0");
|
||||
logger.info("File Info: \n" + imageFile.getFileInfo());
|
||||
}
|
||||
|
||||
public static ImageFile createImageFile(String name, int height, int width, byte[] content, String version) {
|
||||
ImageFile imageFile = new ImageFile(name, height, width, content, version);
|
||||
logger.info("File 2 Info: \n" + imageFile.getFileInfo());
|
||||
return imageFile;
|
||||
}
|
||||
|
||||
public static GenericFile createTextFile(String name, String content, String version) {
|
||||
GenericFile file1 = new TextFile(name, content, version);
|
||||
logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n");
|
||||
return file1;
|
||||
}
|
||||
|
||||
public static TextFile createTextFile2(String name, String content, String version) {
|
||||
TextFile file1 = new TextFile(name, content, version);
|
||||
logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n");
|
||||
return file1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
|
||||
public class BaseMethodClass {
|
||||
|
||||
public static void printMessage() {
|
||||
System.out.println("base static method");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
|
||||
public class ChildMethodClass extends BaseMethodClass {
|
||||
|
||||
public static void printMessage() {
|
||||
System.out.println("child static method");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
public class MethodHidingDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ChildMethodClass.printMessage();
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/7/2018.
|
||||
*/
|
||||
public class ChildVariable extends ParentVariable {
|
||||
|
||||
String instanceVariable = "child variable";
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(instanceVariable);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/6/2018.
|
||||
*/
|
||||
public class HideVariable {
|
||||
|
||||
private String message = "this is instance variable";
|
||||
|
||||
HideVariable() {
|
||||
String message = "constructor local variable";
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public void printLocalVariable() {
|
||||
String message = "method local variable";
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(this.message);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/7/2018.
|
||||
*/
|
||||
public class ParentVariable {
|
||||
|
||||
String instanceVariable = "parent variable";
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(instanceVariable);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/6/2018.
|
||||
*/
|
||||
public class VariableHidingDemo {
|
||||
public static void main(String[] args) {
|
||||
HideVariable variable = new HideVariable();
|
||||
variable.printLocalVariable();
|
||||
variable.printInstanceVariable();
|
||||
|
||||
ParentVariable parentVariable = new ParentVariable();
|
||||
ParentVariable childVariable = new ChildVariable();
|
||||
|
||||
parentVariable.printInstanceVariable();
|
||||
childVariable.printInstanceVariable();
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CastingUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenPrimitiveConverted_thenValueChanged() {
|
||||
double myDouble = 1.1;
|
||||
int myInt = (int) myDouble;
|
||||
|
||||
assertNotEquals(myDouble, myInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcast_thenInstanceUnchanged() {
|
||||
Cat cat = new Cat();
|
||||
Animal animal = cat;
|
||||
animal = (Animal) cat;
|
||||
assertTrue(animal instanceof Cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcastToObject_thenInstanceUnchanged() {
|
||||
Object object = new Animal();
|
||||
assertTrue(object instanceof Animal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcastToInterface_thenInstanceUnchanged() {
|
||||
Mew mew = new Cat();
|
||||
assertTrue(mew instanceof Cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcastToAnimal_thenOverridenMethodsCalled() {
|
||||
List<Animal> animals = new ArrayList<>();
|
||||
animals.add(new Cat());
|
||||
animals.add(new Dog());
|
||||
new AnimalFeeder().feed(animals);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDowncastToCat_thenMeowIsCalled() {
|
||||
Animal animal = new Cat();
|
||||
((Cat) animal).meow();
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void whenDownCastWithoutCheck_thenExceptionThrown() {
|
||||
List<Animal> animals = new ArrayList<>();
|
||||
animals.add(new Cat());
|
||||
animals.add(new Dog());
|
||||
new AnimalFeeder().uncheckedFeed(animals);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDowncastToCatWithCastMethod_thenMeowIsCalled() {
|
||||
Animal animal = new Cat();
|
||||
if (Cat.class.isInstance(animal)) {
|
||||
Cat cat = Cat.class.cast(animal);
|
||||
cat.meow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenParameterCat_thenOnlyCatsFed() {
|
||||
List<Animal> animals = new ArrayList<>();
|
||||
animals.add(new Cat());
|
||||
animals.add(new Dog());
|
||||
AnimalFeederGeneric<Cat> catFeeder = new AnimalFeederGeneric<Cat>(Cat.class);
|
||||
List<Cat> fedAnimals = catFeeder.feed(animals);
|
||||
|
||||
assertTrue(fedAnimals.size() == 1);
|
||||
assertTrue(fedAnimals.get(0) instanceof Cat);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
import com.baeldung.inheritance.*;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
public class AppUnitTest extends TestCase {
|
||||
|
||||
public AppUnitTest(String testName) {
|
||||
super( testName );
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(AppUnitTest.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void testStaticMethodUsingBaseClassVariable() {
|
||||
Car first = new ArmoredCar();
|
||||
assertEquals("Car", first.msg());
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void testStaticMethodUsingDerivedClassVariable() {
|
||||
ArmoredCar second = new ArmoredCar();
|
||||
assertEquals("ArmoredCar", second.msg());
|
||||
}
|
||||
|
||||
public void testAssignArmoredCarToCar() {
|
||||
Employee e1 = new Employee("Shreya", new ArmoredCar());
|
||||
assertNotNull(e1.getCar());
|
||||
}
|
||||
|
||||
public void testAssignSpaceCarToCar() {
|
||||
Employee e2 = new Employee("Paul", new SpaceCar());
|
||||
assertNotNull(e2.getCar());
|
||||
}
|
||||
|
||||
public void testBMWToCar() {
|
||||
Employee e3 = new Employee("Pavni", new BMW());
|
||||
assertNotNull(e3.getCar());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PolymorphismUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenImageFile_whenFileCreated_shouldSucceed() {
|
||||
ImageFile imageFile = FileManager.createImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString()
|
||||
.getBytes(), "v1.0.0");
|
||||
assertEquals(200, imageFile.getHeight());
|
||||
}
|
||||
|
||||
// Downcasting then Upcasting
|
||||
@Test
|
||||
public void givenTextFile_whenTextFileCreatedAndAssignedToGenericFileAndCastBackToTextFileOnGetWordCount_shouldSucceed() {
|
||||
GenericFile textFile = FileManager.createTextFile("SampleTextFile", "This is a sample text content", "v1.0.0");
|
||||
TextFile textFile2 = (TextFile) textFile;
|
||||
assertEquals(6, textFile2.getWordCount());
|
||||
}
|
||||
|
||||
// Downcasting
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void givenGenericFile_whenCastToTextFileAndInvokeGetWordCount_shouldFail() {
|
||||
GenericFile genericFile = new GenericFile();
|
||||
TextFile textFile = (TextFile) genericFile;
|
||||
System.out.println(textFile.getWordCount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user