class Parent { public int parentVariable = 10; public void parentFunction() { System.out.println( "Parent Function" ); } } class Child extends Parent { public void childFunction() { parentFunction(); System.out.println( "In Child " + parentVariable ); } } class Inheritance { public static void main( String args[] ) { Child example = new Child(); example.childFunction(); example.parentFunction(); System.out.println( example.parentVariable ); } }
class Parent { public String name = "Parent"; public int test; public void parentPrint() { System.out.println( name ); } } class Child extends Parent { public String name = "Child"; public String test; public void print() { System.out.println( name ); } } class HiddenFields { public static void main( String args[] ) { Child whoAmI = new Child(); whoAmI.print(); whoAmI.parentPrint(); System.out.println( whoAmI.name ); System.out.println( ( (Parent ) whoAmI).name ); } }
class Parent { public void print() { System.out.println( "In Parent" ); } } class Child extends Parent { public void print() { System.out.println( "In Child" ); } } class OverriddingMethods { public static void main( String args[] ) { Child whoAmI = new Child(); whoAmI.print(); ( ( Parent ) whoAmI).print(); } }
class Parent { public String name = "Parent"; public void print() { System.out.println( "In Parent \t" + name ); } } class Child extends Parent { public int name = 5; public void print() { System.out.println( "In Child \t" + name ); } } class StaticVersesDynamic { public static void main( String args[] ) { Child prodigy = new Child(); prodigy.print(); //Prints In Child 5 Console.println( prodigy.name ); //Prints 5 Parent confused = new Child(); confused.print(); //Prints In Child 5 Console.println( confused.name ); //Prints Parent Parent ok = new Parent(); ok.print(); //Prints In Parent Parent Console.println( ok.name ); //Prints Parent } }
class Parent { private String name = "Parent"; public void print() { System.out.println( "In Parent" ); } public void whichGetsCalled() { System.out.println( "In whichGetsCalled " + name ); print(); } } class Child extends Parent { private String name = "Child"; public void print() { System.out.println( "In Child" ); } } class Polymorphism { public static void main( String args[] ) { Parent whoAmI = new Child(); whoAmI.whichGetsCalled(); whoAmI = new Parent(); whoAmI.whichGetsCalled(); } }
class Parent { public void print() { System.out.println( "In Parent" ); } public void print( String message ) { System.out.println( "In Parent" + '\t' + message ); } public void print( int value ) { System.out.println( "In Parent" + '\t' + value ); } } class Child extends Parent { public int print() { // Compiler Error System.out.println( "In Child" ); return 5; } public void print( String message ) { System.out.println( "In Child" + '\t' + message ); } } class ReturnTypesCount { public static void main( String args[] ) { Child whoAmI = new Child(); whoAmI.print(); whoAmI.print( "Hi Mom" ); whoAmI.print( 10 ); // Ok in Java, // Compile error in C++ } }
class Parent { String name = "Parent"; } class Child extends Parent { String name = "Child"; public void print() { System.out.println( name ); System.out.println( super.name ); System.out.println( Parent.name ); } } class SuperMain { public static void main( String args[] ) { Child whoAmI = new Child(); whoAmI.print(); } }
class Parent { public String name = "Parent"; public void print() { System.out.println( "In Parent" ); } } class Child extends Parent { public String name = "Child"; public void print() { System.out.println( "In Child" ); } public void superTest() { System.out.println( "In Child test " + super.name ); super.print(); } } class GrandChild extends Child { public String name = "Child"; public void print() { System.out.println( "In GrandChild" ); } } class SuperIsStatic { public static void main( String args[] ) { GrandChild whoAmI = new GrandChild(); whoAmI.superTest(); } }
class Parent { String name = "Parent"; public void print() {System.out.println( "In Parent" + name ); } public void thisFunction() { System.out.println( this.name ); this.print(); } } class Child extends Parent { String name = "Child"; public void print() { System.out.println( "In Child " + name ); } } class This { public static void main( String args[] ) { Parent parentThis = new Child(); parentThis.thisFunction(); Child childThis = new Child(); childThis.thisFunction(); } }
class Parent { public static void classFunction() { System.out.println( "In Parent" ); } public static void inheritMe() { System.out.println( "Inherited" ); } } class Child extends Parent { public static void classFunction() { System.out.println( "In Child" ); } } class StaticTest { public static void main( String args[] ) { Parent exam = new Child(); exam.classFunction(); Child question = new Child(); question.classFunction(); question.inheritMe(); } }
class Private { private String name = "Private"; public void print() { System.out.println( name ); } public void tricky( Private argument ) { argument.name = "This access is legal"; } } class Excluded extends Private { public void cantPrint() { System.out.println( name ); // Compile error } } class PrivateExample { public static void main( String[] args ) { Private top = new Private(); Private bottom = new Private(); top.tricky( bottom ); bottom.print(); } }
package Botany; public class Protected { protected String name = "Protected"; } package Botany; public class NoRelation { public void SampleAccess( Protected accessOK ) { accessOK.name = "This is legal"; } } package Tree; public class NoRelationEither { public void SampleAccess( Botany.Protected noAccess) { noAccess.name = "This is a compile Error"; } } package Tree; public class Child extends Botany.Protected { public void SampleAccess( Botany.Protected noAccess, Child accessOK) { name = "This is legal, I am related"; noAccess.name = "This is a compile Error"; accessOK.name = "This is legal"; } }
package Botany; public class NoExplicit { String name = "No explicit access level given"; } package Botany; public class NoRelation { public void SampleAccess( NoExplicit accessOK ) { accessOK.name = "This is legal"; } } package Tree; public class NoRelationEither { public void SampleAccess( Botany.NoExplicit noAccess) { noAccess.name = "This is a compile Error"; } } package Tree; public class Child extends Botany.NoExplicit { public void SampleAccess( Botany.NoExplicit noAccess, Child alsoNoAccess) { name = "I am related, but this is NOT LEGAL"; noAccess.name = "This is a compile Error"; alsoNoAccess.name = "This is a compile Error"; } }
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 Parent { public Parent() { System.out.println( "In Parent" ); } } class Child extends Parent { public Child() { System.out.println( "In Child" ); } public Child( String notUsed ) { System.out.println( "In Child with argument" ); } } class Constructors { public static void main( String args[] ) { System.out.println( "Construct Parent" ); Parent sleepy = new Parent(); System.out.println( "Construct Child" ); Child care = new Child(); care = new Child( "Try Me" ); } }
Construct Parent In Parent Construct Child In Parent In Child In Parent In Child with argument
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 ); } }
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? } }
import sdsu.io.Console; class Foobar { public String name = "Skip if this confuses you"; } class DealingWithClasses { public static void main( String[] arguments ) throws ClassNotFoundException, IllegalAccessException, InstantiationException { Foobar example = new Foobar(); Console.println( example.getClass().getName() ); Console.println( example.getClass().getSuperclass() ); // Stop reading while it is still safe // Creating an object from a string example = (Foobar) Class.forName( "Foobar" ).newInstance(); String whichClass = "java.util.Vector"; Object interesting = Class.forName( whichClass ).newInstance(); Console.println( interesting.getClass().getName() ); whichClass = Console.readLine( "Enter a class name" ); interesting = Class.forName( whichClass ).newInstance(); Console.println( interesting.getClass().getName() ); } }
class Parent { int data; } class Child extends Parent { String name; } class Uncle { String rich; } class Casting { public static void main( String args[] ) { Object object; Parent parent; Child child = new Child(); Uncle uncle; parent = child; object = child; parent = (Parent) object; child = (Child) object; uncle = (Uncle) object; //Runtime exception }}
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 ); } }
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; } }
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 } }
public interface Left { public void truth(); } public interface Right { public boolean truth(); } class Congress implements Left, Right { public ???? truth() { } }