<commit_before>package beginner;

import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
import static com.sandwich.util.Assert.fail;

import com.sandwich.koan.Koan;

@SuppressWarnings("unused")
public class AboutCasting {
    
	@Koan
	public void longPlusInt() {
		int a = 6;
		long b = 10;
		Object c = a + b;
		assertEquals(c, __);
		assertEquals(c instanceof Integer, __);
		assertEquals(c instanceof Long, __);
	}
    
    @Koan
	public void forceIntTypecast() {
		long a = 2147483648L; 
		// What happens if we force a long value into an int?
		int b = (int)a;
		assertEquals(b, __);
	}
   
    @Koan
	public void implicitTypecast() {
    	int a = 1;
    	int b = Integer.MAX_VALUE;
		long c = a + b; // still overflows int... which is the Integer.MIN_VALUE, the operation occurs prior to assignment to long
		assertEquals(c, __);
	}
    
    interface Sleepable {
    	String sleep();
    }
    class Child implements Sleepable{
    	public String sleep() { 
    		return "zzzz"; 
    	}
    }
    class Parent extends Child { 
    	public String complain() { 
    		return "TPS reports don't even have a cover letter!"; 
    	} 
    }
    class GrandParent extends Parent { 
    	public String complain() { 
    		return "Get your feet off the davenport!"; 
    	} 
    }
    
    @Koan
    public void downCastWithInerhitance() {
    	GrandParent grandParent = new GrandParent();
    	Parent parentReference = grandParent; // Why isn't there an explicit cast?
    	assertEquals(grandParent instanceof GrandParent, __);
    	assertEquals(parentReference instanceof GrandParent, __);
    	assertEquals(parentReference instanceof Parent, __);
    	assertEquals(parentReference instanceof Child, __);
    }
    
    @Koan
    public void downCastAndPolymophism() {
    	GrandParent grandParent = new GrandParent();
    	Parent parentReference = grandParent;
    	// If the result is unexpected, consider the difference between an instance and its reference
    	assertEquals(parentReference.complain(), __);
    }
    
    @Koan
    public void upCastWithInheritance() {
    	Child grandParent = new GrandParent();
    	Parent parentReference = (Parent)grandParent; // Why do we need an explicit cast here?
    	GrandParent grandParentReference = (GrandParent)parentReference; // Or here?
    	assertEquals(grandParentReference instanceof GrandParent,__);
    	assertEquals(grandParentReference instanceof Parent,__);
    	assertEquals(grandParentReference instanceof Child,__);
    }
    
    @Koan
    public void upCastAndPolymophism() {
    	Child grandParent = new GrandParent();
    	Parent parent = (GrandParent)grandParent;
    	// Think about the result. Did you expect that? Why?
    	// How is that different from above?
    	assertEquals(parent.complain(), __);
    }
    
    @Koan
    public void classCasting(){
    	try{
    		Object o = new Object(); 	// were downcasting way to far here - would it be possible
    									// to even compile this koan had we done what was safe, and 
    									// held the reference as Sleepable?
    		((Sleepable)o).sleep();
    	}catch(ClassCastException x){
    		fail("Object does not implement Sleepable, maybe one of the people classes do?");
    	}
    }
    
    @Koan
    public void complicatedCast() {
    	Child parent = new Parent();
    	// How can we access the parent's ability to "complain" - if the reference is held as a superclass? 
    	assertEquals(new Parent().complain(), __);
    }
   
}
<commit_msg>Inverting inheritance tree in AboutCasting to make it less confusing<commit_after>package beginner;

import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
import static com.sandwich.util.Assert.fail;

import com.sandwich.koan.Koan;

@SuppressWarnings("unused")
public class AboutCasting {

	@Koan
	public void longPlusInt() {
		int a = 6;
		long b = 10;
		Object c = a + b;
		assertEquals(c, __);
		assertEquals(c instanceof Integer, __);
		assertEquals(c instanceof Long, __);
	}

	@Koan
	public void forceIntTypecast() {
		long a = 2147483648L;
		// What happens if we force a long value into an int?
		int b = (int) a;
		assertEquals(b, __);
	}

	@Koan
	public void implicitTypecast() {
		int a = 1;
		int b = Integer.MAX_VALUE;
		long c = a + b; // still overflows int... which is the Integer.MIN_VALUE, the operation occurs prior to assignment to long
		assertEquals(c, __);
	}

	interface Sleepable {
		String sleep();
	}

	class Grandparent implements Sleepable {
		public String sleep() {
			return "zzzz";
		}
	}

	class Parent extends Grandparent {
		public String complain() {
			return "TPS reports don't even have a cover letter!";
		}
	}

	class Child extends Parent {
		public String complain() {
			return "Are we there yet!!";
		}
	}

	@Koan
	public void downCastWithInheritance() {
		Child child = new Child();
		Parent parentReference = child; // Why isn't there an explicit cast?
		assertEquals(child instanceof Child, __);
		assertEquals(parentReference instanceof Child, __);
		assertEquals(parentReference instanceof Parent, __);
		assertEquals(parentReference instanceof Grandparent, __);
	}

	@Koan
	public void downCastAndPolymorphism() {
		Child child = new Child();
		Parent parentReference = child;
		// If the result is unexpected, consider the difference between an instance and its reference
		assertEquals(parentReference.complain(), __);
	}

	@Koan
	public void upCastWithInheritance() {
		Grandparent child = new Child();
		Parent parentReference = (Parent) child; // Why do we need an explicit cast here?
		Child childReference = (Child) parentReference; // Or here?
		assertEquals(childReference instanceof Child, __);
		assertEquals(childReference instanceof Parent, __);
		assertEquals(childReference instanceof Grandparent, __);
	}

	@Koan
	public void upCastAndPolymorphism() {
		Grandparent child = new Child();
		Parent parent = (Child) child;
		// Think about the result. Did you expect that? Why?
		// How is that different from above?
		assertEquals(parent.complain(), __);
	}

	@Koan
	public void classCasting() {
		try {
			Object o = new Child();		// were downcasting way to far here - would it be possible
										// to even compile this koan had we done what was safe, and
										// held the reference as Sleepable?
			((Sleepable) o).sleep();
		} catch (ClassCastException x) {
			fail("Object does not implement Sleepable, maybe one of the people classes do?");
		}
	}

	@Koan
	public void complicatedCast() {
		Grandparent parent = new Parent();
		// How can we access the parent's ability to "complain" - if the reference is held as a superclass?
		assertEquals("TPS reports don't even have a cover letter!", __);
	}

}

{'fraction_non_alphanumeric': 0.09174041297935104, 'fraction_numerical': 0.004129793510324484, 'mean_word_length': 3.2782334384858043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4806358', 'n_tokens_mistral': 2155, 'n_tokens_neox': 1958, 'n_words': 671}