class Parent { int parentVariable; public void parentFunction() { System.out.println( "Parent Function" ); } public void overRideMe() { System.out.println( "In Parent" ); } } class Child extends Parent { public void overRideMe() { System.out.println( "In Child" ); } } class Inheritance { public static void main( String args[] ) { Parent Polymorphism = new Child(); Polymorphism.overRideMe(); Polymorphism.parentFunction(); } }
class Parent { String name = "Parent"; } class Child extends Parent { String name = "Child"; public void print() { System.out.println( name ); System.out.println( this.name ); System.out.println( super.name ); System.out.println( Parent.name ); System.out.println( ((Parent)this).name ); } } class SuperThis { public static void main( String args[] ) { Child whoAmI = new Child(); whoAmI.print(); } }
class Parent { String name = "Parent"; public void thisFunction() { System.out.println( this.name ); } } class Child extends Parent { String name = "Child"; public void thisFunction() { System.out.println( this.name ); } } class SuperThis { public static void main( String args[] ) { Parent polymorphism = new Child(); polymorphism.thisFunction(); polymorphism = new Parent(); polymorphism.thisFunction(); } }
class Parent { String name = "Parent"; public void thisFunction() { System.out.println( this.name ); } } class Child extends Parent { String name = "Child"; } class StaticThis { public static void main( String args[] ) { Parent parentType = new Child(); parentType.thisFunction(); Child childType = new Child(); childType.thisFunction(); } }
class Parent { public void fancy() { whichOne(); } public void whichOne() { System.out.println( "Parent" ); } } class Child extends Parent { public void whichOne() { System.out.println( "Child" ); } } class VirtualFunctionsByDefault { public static void main( String args[] ) { Parent polymorphism = new Child(); polymorphism.fancy(); polymorphism = new Parent(); polymorphism.fancy(); } }
Is short hand for
class Parent { int size; } class TestObject { public static void main( String args[] ) { Parent watchThis = new Parent(); int myHash = watchThis.hashCode(); System.out.println( myHash ); // Where does hashCode come from? } }
class Parent { public static void classFunction() { System.out.println( "In Parent" ); } } class Child extends Parent { public static void classFunction() { System.out.println( "In Child" ); } } class StaticTest { public static void main( String args[] ) { Parent finalExam = new Child(); finalExam.classFunction(); Parent.classFunction(); } }
final class EndOfTheLine { int noSubclassPossible; public void aFunction() { System.out.println( "Hi Mom" ); } } abstract class NoObjects { int noObjectPossible; public void aFunction() { System.out.println( "Hi Mom" ); } public abstract void subClassMustImplement( int foo ); } public class Bar { // Must be in file called Bar.java int canBeAccessedFromOtherPackages; public void aFunction() { System.out.println( "Hi Mom" ); } } class NoModifier { // Can be accessed from this package only int whatDoesThisMean; public void aFunction() { System.out.println( "Hi Mom" ); } }
abstract class NoObjects { int noDirectObjectPossible = 5; public void aFunction() { System.out.println( "Hi Mom" ); } public abstract void subClassMustImplement( int foo ); } class Concrete extends NoObjects { public void subClassMustImplement( int foo ) { System.out.println( "In Concrete" ); } } class AbstractClasses { public static void main( String args[] ) { NoObjects useful = new Concrete(); Concrete thisWorks = new Concrete(); useful.subClassMustImplement( 10 );
System.out.println( thisWorks.noDirectObjectPossible ); } }
final class EndOfTheLine { int noSubclassPossible; public void aFunction() { System.out.println( "Hi Mom" ); } }
class ThisWillNotWork extends EndOfTheLine { int ohNo; }Does not compile
class Parent { public final void theEnd() { System.out.println( "This is it" ); } public void normal() { System.out.println( "In parent" ); } } class Child extends Parent { public void theEnd() { // Compile Error System.out.println( "Two Ends?" ); } public void normal() { System.out.println( "In Child" ); } }
class ConstructorExample { public ConstructorExample( ) { System.out.println( "In constructor - no argument" ); }; public ConstructorExample( int size) { System.out.println( "In constructor - one argument" ); }; public void ConstructorExample( ) { System.out.println( " return type means it is "); System.out.println( "not a constructor " ); }; } class TestConstructor { public static void main( String args[] ) { System.out.println( " Start main " ); ConstructorExample test = new ConstructorExample( ); ConstructorExample x = new ConstructorExample(5); System.out.println( " Done with Constructors " ); test.ConstructorExample (); } }
class Parent { public Parent() { System.out.println( "In Parent" ); } } class Child extends Parent { public Child() { System.out.println( "In Child" ); } } class Constructors { public static void main( String args[] ) { System.out.println( "Construct Parent" ); Parent polymorphism = new Parent(); System.out.println( "Construct Child" ); Child care = new Child(); } }
class ImplicitConstructorOnly { int size = 5; } class OneConstructor { OneConstructor( String message ) { System.out.println( message ); } } class TwoConstructors { TwoConstructors ( String message ) { System.out.println( message ); } TwoConstructors ( ) { System.out.println( "No argument Constructor" ); } } class Constructors { public static void main( String args[] ) { ImplicitConstructorOnly ok = new ImplicitConstructorOnly(); TwoConstructors alsoOk = new TwoConstructors(); OneConstructor compileError = new OneConstructor(); } }
class Parent { public Parent( ) { System.out.println( "In Parent, No Argument" ); } public Parent( String message ) { System.out.println( "In Parent" + message ); } } class Child extends Parent { public Child( String message, int value ) { this( message ); // if occurs must be first System.out.println( "In Child" ); } public Child( String message ) { super( message ); // if occurs must be first System.out.println( "In Child" + message ); } } class Constructors { public static void main( String args[] ) { System.out.println( "Construct Child" ); Child care = new Child( "Start from Child", 5 ); } }
class Parent { public Parent( ) { System.out.println( "In Parent, No Argument" ); } public Parent( String message ) { System.out.println( "In Parent" + message ); } } class Child extends Parent { public Child( String message, int value ) { this( message ); // overrides implicit call System.out.println( "In Child" ); } public Child( String message ) { System.out.println( "In Child" + message ); } } class Constructors { public static void main( String args[] ) { System.out.println( "Construct Child" ); Child care = new Child( "Start from Child", 5 ); } }
class Death { int myId; public Death ( int sequenceNumber) { myId = sequenceNumber; } public void finalize( ) { System.out.println( myId ); } } class Finalization { public static void main( String args[] ) { Death sampleObject; for ( int k = 0; k < 5; k++ ) sampleObject = new Death( k ); } }
class FinalizationForced { public static void main( String args[] ) { Death sampleObject; for ( int k = 0; k < 5; k++ ) sampleObject = new Death( k ); System.gc(); System.runFinalization(); } }
public interface Flyer { public boolean hasWings(); }
interface Mammal { static boolean hasLungs = true; public boolean hasLegs(); } interface Noisy extends Mammal { public boolean hasBark(); } class Dog implements Noisy { public boolean hasLegs() { return true; } public boolean hasBark() { return true; } }// file test.java continued
class Bird implements Mammal, Flyer { public boolean hasLegs(){ return true; } public boolean hasWings() { return true; } } class InterfaceExample { public static void main( String args[] ) { Bird robin = new Bird(); Flyer crow = new Bird(); Noisy fido = new Dog(); Mammal fifi = new Noisy(); //Compiler Error robin.hasLegs(); robin.hasWings(); boolean test = robin.hasLungs; crow.hasLegs(); //Compile Error } }
class SimpleExceptionDoneWrong { public void compute( ) { throw new Exception( "Bad programmer" ); //Compile error } public void politeCompute( ) throws Exception { //Ok throw new Exception( "Catch anyone?" ); } public void safeCompute( ) { // Ok try { throw new Exception( "Catch anyone?" ); } catch (Exception e) { // Do the right thing here } } }
class SimpleException{ public void compute( ) throws Exception { throw new Exception( "Bad programmer" ); } public void callCompute() throws Exception { // OK compute(); } public void handleTheException() { // OK try { compute(); } catch ( Exception e ) { System.err.println( " Math Error" + e.getMessage() ); } } public void thisDoesNotCompile() { compute(); // Compile Error } }
class SimpleExceptionOK { public void compute( ) {//OK throw new ArithmeticException( "this works" ); } public void moreCompute( ) {//OK throw new ArrayIndexOutOfBoundsException( "this works"); } }
java.applet | java.awt.peer | java.net |
java.awt | java.io | java.util |
java.awt.image | java.lang | sun.tools.debug |
class Output { public static void main( String args[] ) { java.io.PrintStream out = System.out; out.print( "Look Mom, No System" ); } }
import java.io.PrintStream; class Output { public static void main( String args[] ) { PrintStream out = System.out; out.print( "Look Mom, No System" ); } }
import java.io.*; class Output { public static void main( String args[] ) { PrintStream out = System.out; out.print( "Look Mom, No System" ); } }
package sdsu.roger; public class Sample { public void hello() { System.out.println( "Hello for package sdsu.roger" ); } }Place program in file named "Sample.java"
import sdsu.roger.Sample; class TestPackage { public static void main( String args[] ) { Sample me = new Sample(); me.hello(); } }