Lecture19

Lecture19 - All constant values defined in an interface are...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 interface Data1 { public final int x = 1; // constant public int y = 2; // constant public int calc1(); // abstract method public int calc2(); // abstract method } interface Data2 { public final int z = 3; // constant } class DataA implements Data1 { public int calc1() { return x+y; } public int calc2() { return x*y; } } class DataB implements Data1, Data2 { public int calc1() { return x+y+z; } public int calc2() { return x*y*z; } } public class interface_basics { public static void main(String[] args) { System.out.println(new DataA().calc1()); System.out.println(new DataB().calc1()); }} How about? interface Data2 { public final int y = 3; public final int z = 3; // constant } class DataB implements Data1, Data2 { public int calc1() { return x+Data2.y+z; } public int calc2() { return x*Data2.y+z; } } All constant values defined in an interface are implicitly public, static, and final. All methods declared in an interface are implicitly public and abstract.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 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(); } }
Background image of page 2
3 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(); } } Parent fancy() WhichOne() Child WhichOne()
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 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); } } useful.aFunction();??
Background image of page 4
5 class Figure { void display( ) { System.out.println("Figure"); } } class Rectangle extends Figure { void display( ) { System.out.println("Rectangle"); } } class Box extends Figure { void display( ) { System.out.println("Box"); } } class Inherit { public static void main(String[ ] args) { Figure f = new Figure( ); Rectangle r = new Rectangle( ); Box b = new Box( ); f.display( ); f = r; f.display( ); f = b; f.display( ); }} a) Figure Rectangle Box b) Rectangle Box c) Figure Figure Figure d) Syntax error. This code won't compile or execute e) None of the above
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 abstract class Figure { void display( )
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

Lecture19 - All constant values defined in an interface are...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online