2-dimensional array
1-, 2-, and higher-dimensional arrays
Java, as with most languages, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional, ... In practice most arrays are one-dimensional, and two-dimensional (rows and columns) are also quite common. Higher dimensional arrays are less common so they aren't used in the examples, but there's nothing mysterious about them and the same principles apply.
Two-dimensional arrays are used whenever the model data is best represented with rows and columns, or has two varying aspects (eg, gender and age, weight and height, ...). It's also the idea, altho not the implementation, of graphics that specifies a two (or three) dimensional position with an x and y (and z) coordinate.
Terminology. Other terms you will see for a two-dimensional array are matrix or table.
Visualizing two-dimensional arrays
2-dimensional arrays are usually represented with a row-column "spreadsheet" style. Assume we have an array,
a, with two rows and four columns.int[][] a = new int[2][4]; // Two rows and four columns.
| Two-dimensional arrays are usually visualized as a matrix, with rows and columns. This diagram shows the array a with its corresponding subscripts. |
Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
When we talk about inheritance, the most commonly used keyword would be extends andimplements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.
IS-A Relationship:
IS-A is a way of saying : This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
public class Animal{ } public class Mammal extends Animal{ } public class Reptile extends Animal{ } public class Dog extends Mammal{ }
Now, based on the above example, In Object Oriented terms, the following are true:
- Animal is the superclass of Mammal class.
- Animal is the superclass of Reptile class.
- Mammal and Reptile are subclasses of Animal class.
- Dog is the subclass of both Mammal and Animal classes.
Now, if we consider the IS-A relationship, we can say:
- Mammal IS-A Animal
- Reptile IS-A Animal
- Dog IS-A Mammal
- Hence : Dog IS-A Animal as well
Inheritance and constructors in Java
December 21, 2010
In Java, constructor of base class with no argument gets automatically called in derived class constructor. For example, output of following program is:
Base Class Constructor Called
Derived Class Constructor Called
But, if we want to call parameterized contructor of base class, then we can call it using super(). The point to note is base class comstructor call must be the first line in derived class constructor. For example, in the following program, super(_x) is first line derived class constructor.
Output:
x = 10, y = 20
Base Class Constructor Called
Derived Class Constructor Called
// filename: Main.javaclass Base { Base() { System.out.println("Base Class Constructor Called "); }}class Derived extends Base { Derived() { System.out.println("Derived Class Constructor Called "); }}public class Main { public static void main(String[] args) { Derived d = new Derived(); }} |
// filename: Main.javaclass Base { int x; Base(int _x) { x = _x; }}class Derived extends Base { int y; Derived(int _x, int _y) { super(_x); y = _y; } void Display() { System.out.println("x = "+x+", y = "+y); }}public class Main { public static void main(String[] args) { Derived d = new Derived(10, 20); d.Display(); }} |
x = 10, y = 20
Implicit super constructor is undefined for default constructor
abstract public class BaseClass {
String someString;
public BaseClass(String someString) {
this.someString = someString;
}
abstract public String getName();
}
public class ACSubClass extends BaseClass {
public ASubClass(String someString) {
super(someString);
}
public String getName() {
return "name value for ASubClass";
}
}
I will have quite a few subclasses of
BaseClass, each implementing the getName() method in its own way (template method pattern).
This works well, but I don't like having the redundant constructor in the subclasses. It's more to type and it is difficult to maintain. If I were to change the method signature of the
BaseClass constructor, I would have to change all the subclasses.
No comments:
Post a Comment