diff --git a/castingandothers/pom.xml b/castingandothers/pom.xml new file mode 100644 index 0000000..074fc54 --- /dev/null +++ b/castingandothers/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + com.example + castingandothers + 1.0-SNAPSHOT + + + 17 + 17 + + + \ No newline at end of file diff --git a/castingandothers/src/main/java/com/example/Animal.java b/castingandothers/src/main/java/com/example/Animal.java new file mode 100644 index 0000000..3b75755 --- /dev/null +++ b/castingandothers/src/main/java/com/example/Animal.java @@ -0,0 +1,8 @@ +package com.example; + +class Animal{ + public String name; + public void Animal(){ + this.name = "Something!!!"; + } +} \ No newline at end of file diff --git a/castingandothers/src/main/java/com/example/Cat.java b/castingandothers/src/main/java/com/example/Cat.java new file mode 100644 index 0000000..1e0fc07 --- /dev/null +++ b/castingandothers/src/main/java/com/example/Cat.java @@ -0,0 +1,12 @@ +package com.example; + +public class Cat extends Animal { + public String sound; + public void Cat(){ + this.sound = "MEEEEOW"; + } + public void makesound(){ + this.sound = "Something like MEEEEOW"; + System.err.println(sound); + } +} diff --git a/castingandothers/src/main/java/com/example/Main.java b/castingandothers/src/main/java/com/example/Main.java new file mode 100644 index 0000000..6c5fbe2 --- /dev/null +++ b/castingandothers/src/main/java/com/example/Main.java @@ -0,0 +1,41 @@ +package com.example; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + + double asd = 3.5423; + int castedasd = (int) asd; + System.out.println(castedasd); + System.out.print("Gimme a letter and Ill tel you the ASCII code! "); + Scanner read = new Scanner(System.in); + char a = read.next().charAt(0); + + //your code goes here + System.out.println((int)a); + + + // Upcasting + // You can cast an instance of a subclass to its superclass. + + // Consider the following example, assuming that Cat is a subclass of Animal. + Animal C = new Cat(); + + Animal B = new Cat(); + System.out.println(((Cat)B).sound); + + // Anonymous Classes + // Anonymous classes are a way to extend the existing classes on the fly. + + // For example, consider having a class Machine: + Cat c = new Cat(){ + @Override public void makesound(){ + System.err.println("MOOOOOOOOOOOOOO"); + } + }; + + c.makesound(); + } +} \ No newline at end of file diff --git a/castingandothers/target/classes/com/example/Animal.class b/castingandothers/target/classes/com/example/Animal.class new file mode 100644 index 0000000..830297c Binary files /dev/null and b/castingandothers/target/classes/com/example/Animal.class differ diff --git a/castingandothers/target/classes/com/example/Cat.class b/castingandothers/target/classes/com/example/Cat.class new file mode 100644 index 0000000..ce4c4cb Binary files /dev/null and b/castingandothers/target/classes/com/example/Cat.class differ diff --git a/castingandothers/target/classes/com/example/Main$1.class b/castingandothers/target/classes/com/example/Main$1.class new file mode 100644 index 0000000..927e7e7 Binary files /dev/null and b/castingandothers/target/classes/com/example/Main$1.class differ diff --git a/castingandothers/target/classes/com/example/Main.class b/castingandothers/target/classes/com/example/Main.class new file mode 100644 index 0000000..304f426 Binary files /dev/null and b/castingandothers/target/classes/com/example/Main.class differ