Wednesday, February 7, 2007

Lecture 13

Inner Classes

  • an inner class is a class defined within another class

Example

public class outerClass
{
private class innerClass
{
}
}
  • inner classes provide extra 'class-like' functionality to your class
    • makes your class look self contained
  • inner classes have access to private members of the outer class
    • and vice versa
  • inner classes can be made public and are then visible outside of the outer class

Access

public class outerClass
{
private int y;
private int x;

private class innerClass
{
private int y;

public void method()
{x=0 //if no private member in inner class named x
}
}
  • if no private member in innerClass is named x , it can refer to x in the outer class by name
  • to gain access to the outer class y
    • outerClass.this.y

public class outerClass
{
private int x;
private int y;
private class innerClass
{
private int y;
public void method()
{
x = 0; // refers to the outerClass x
y = 0; //refers to the innerClass y
outerClass.this.y; // refers to outerClass y
}
}
}

  • creating a public Object of an inner class type
public class outerClass
{
public class innerClass
{
}
}
  • first create an object of type outerClass
  • then create an object of type innerClass
outerClass a = new outerClass();
outerClass.innerClass b = a.new innerClass();

Aggregates Compositions and Arrays


Arrays

  • in undergrad say we want to store the grades for all the term tests instead of just the final raw grade
  • an array lets us refer the collections of variables of the same type using a single name and number
Very Important

  • Arrays in java always have their first object at zero (0)
  • if you don't know the length of an array
    • all arrays have a special public field called 'length'
testArray.length
  • Alternative Array Declaration
    • (with initialization)
double[]testArray = {0.0,0.0,0.0,0.0,0.0}
  • this array has 5 elements

3 comments:

George said...

I don't understand why do arrays start with id "0" and so are loops. I mean we never say the "zero est" element in English instead we say the first, the second and so on. Why is it different in computer languages?

I heard a claim that it is a logic related issue, but does anybody have an explanation.
I personally think zero is not a number but a representation of its absence.

Thank you.

Jake deVos said...

If 0 weren't a number then 1011 would represent 7 becuase 0 wouldn't be a placeholder.
it is infact 11!
i understand what you mean by representation of its absence, but if used in binary as a placeholder it should be logically understandable to use it as a placeholder in java

George said...

Thanks jake,
Could you please post lecture 14.
I missed this one.

Thanks in advance