Back to Basics – Subtler Nuances – Part One

IllegalClass.java

//interfaces do not contain static methods – they are not overridden
public interface IllegalClass {
	public static void illegalMethod();
}

Parent.java

public class Parent {
	public static void toDo() {
		System.out.println("The Parent speaks!!");
	}
}

class Child extends Parent {
	public static void toDo() {
		System.out.println("The Child speaks!!");

	}

	public static void main(String args[]) {
		Parent parent = new Parent();
		parent.toDo();
		Parent child = new Child();
		child.toDo();
		Child childOfParent = new Child();
		childOfParent.toDo();
	}
}
/**
Note static toDo is not overridden. The execution sequence is not looked up at runtime!
And hence static methods cannot be contained in an interface.
C:\appletree>java Parent

The Parent speaks!!
The Parent speaks!!
The Child speaks!!
*/

ITooPromiseImplVarTest.java

class IPromiseImpl {
	String name = "Mattiz";
}

public class ITooPromiseImplVarTest extends IPromiseImpl {
	String name = "Porsche";

	public static void main(String[] args) {
		ITooPromiseImplVarTest iPromiseImpl = new ITooPromiseImplVarTest();
		System.out.println(iPromiseImpl.name);
		IPromiseImpl iPromise = new ITooPromiseImplVarTest();
		System.out.println(iPromise.name);
	}
};
/**
D:\appletree>java ITooPromiseImplVarTest
Porsche
Mattiz
*/

Original.java

public interface Original {
	public void doPicasso();
}

interface Duplicate {
	public void doMattisse(final int var);
}

interface Extender extends Original, Duplicate {
	public void cheapSkatePainting();
}

class AllRounder implements Extender {
	public void doPicasso() {
		System.out.println("Picasso Art Piece");
	}

	public void doMattisse(final int count) {
		System.out.println("Mattisse Art Piece number " + count);
	}

	public void cheapSkatePainting() {
		final int count = 10;
		System.out.println("My sad first attempt at painting " + count);
	}

	public static void main(String args[]) {
		AllRounder ronaldo = new AllRounder();
		ronaldo.doPicasso();
		int count = 11;
		ronaldo.doMattisse(count);
		ronaldo.cheapSkatePainting();
	}
}
/*
An interface can extend multiple interfaces!!

C:\appletree>java Original
Picasso Art Piece
Mattisse Art Piece number 11
My sad first attempt at painting 10
*/

MyPromise.java

public interface MyPromise{
int length = 10;
}
class MyPromiseImpl implements MyPromise
{
int length = MyPromise.length;//compiles fine
MyPromise.length = 25;//does not compile. length is by default - public static final in the interface
};

IPromise.java

public interface IPromise {
	void toDo();
}

class IPromiseImpl implements IPromise {
	private void toDo() {
		System.out.println("Promised");
	}
}
/*

toDo() is by default public in IPromise interface. It is overridden by a class in which toDo has lower visibility - compile time error

D:\appletree>javac IPromise.java
IPromise.java:7: toDo() in IPromiseImpl cannot implement toDo() in IPromise; attempting to assign weaker access privileges; was public
private void toDo(){
^
1 error
*/

IPromiseAnExceptionImpl.java

interface IPromiseAnException {
	public void toDo() throws Exception;
}

public class IPromiseAnExceptionImpl implements IPromiseAnException {
	public static void main(String[] args) {
		IPromiseAnExceptionImpl iPromiseImpl = new IPromiseAnExceptionImpl();// compiles
																				// fine
		iPromiseImpl.toDo();// compiles fine
		IPromiseAnException iPromise = new IPromiseAnExceptionImpl();// compiles
																		// fine
		iPromise.toDo();// compile time error
	}

	public void toDo() {
		System.out.println("Implemented");
	}
}
/*

Compiler sees an exception thrown by parent class, overriding occurs at runtime

D:\appletree>javac IPromiseAnExceptionImpl.java
IPromiseAnExceptionImpl.java:9: unreported exception java.lang.Exception; must be caught or declared to be thrown
iPromise.toDo();
^
1 error
*/

ITooPromiseAnExceptionImpl.java

import java.sql.SQLException;

class IPromiseAnExceptionImpl implements IPromiseAnException {
	public void toDo() throws Exception {
		System.out.println("Implemented");
	}
}

public class ITooPromiseAnExceptionImpl extends IPromiseAnExceptionImpl {
	public void toDo() throws SQLException {
		System.out.println("Done");
	}

	public static void main(String[] args) {
		try {
			ITooPromiseAnExceptionImpl iPromiseImpl = new ITooPromiseAnExceptionImpl();// compiles
																						// fine
			iPromiseImpl.toDo();
			IPromiseAnExceptionImpl iPromise = new ITooPromiseAnExceptionImpl();// compiles
																				// fine
			iPromise.toDo();// runtime selection of execution call in either
							// case.
		} catch (Exception e) {
			// do something
		}
	}
};
/*
SQL Exception subset of Exception - hence no compile time error
D:\appletree>javac ITooPromiseAnExceptionImpl.java

D:\appletree>java ITooPromiseAnExceptionImpl
Done
Done
*/

CastTester.java

//result of int sized or smaller expresssions is always an int
public class CastTester {
	public void toDo() {
		byte b = 2;// valid -> (byte)27
		char c = 8;// valid -> (char)89
		short m = 90;// valid -> (short)90
		byte a = (b + c);
		// expression equates to int, no casting done.. compile time error
	}

	public static void main(String args[]) {
		CastTester castTester = new CastTester();
		castTester.toDo();
	}
}
/*
C:\appletree>javac CastTester.java
CastTester.java:7: possible loss of precision
found   : int
required: byte
byte a = (b+c);
^
1 error
*/

CastTester2.java

//result of int sized or smaller expresssions is always an int
public class CastTester2 {
	public void toDo() {
		byte b = 2;// valid -> (byte)27
		char c = 8;// valid -> (char)89
		short m = 90;// valid -> (short)90
		short a = (b + c);// expression evaluates to int at runtime.. no
							// explicit cast to short
	}

	public static void main(String args[]) {
		CastTester2 castTester2 = new CastTester2();
		castTester2.toDo();
	}
}
/*
C:\appletree>javac CastTester2.java
CastTester2.java:7: possible loss of precision
found   : int
required: short
short a = (b+c);
^
1 error
*/

CastTester3.java

//result of int sized or smaller expresssions is always an int
public class CastTester3 {
	public void toDo() {
		byte b = 2;// valid -> (byte)27
		char c = 8;// valid -> (char)89
		short m = 90;// valid -> (short)90
		float f = 34.4;// not valid double assigned to float..no implicit cast
						// to float
	}

	public static void main(String args[]) {
		CastTester3 castTester3 = new CastTester3();
		castTester3.toDo();
	}
}
/*
C:\appletree>javac CastTester3.java
CastTester3.java:7: possible loss of precision
found   : double
required: float
float f = 34.4;//
^
1 error]
*/

MyOuter.java

public class MyOuter {
	static class MyNested {
		public MyNested() {
			System.out.println("Body of Nested Inner Class");
		}
	}

	public static void main(String args[]) {
		MyOuter.MyNested myNested = new MyOuter.MyNested();
	}
}
/*
C:\appletree>java MyOuter
Body of Nested Inner Class
*/

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: