Undertanding Arrays & Inheritance using Java

An Array is a similar data- type with similar values. In Java, arrays can only be of a single type like Integers, Strings, Float, Double and so on.

For example, consider the following two statements

  1: int array = new int [20];

means array is assigned 20 memory locations or

  1: int comp[] = {10,20,30};

means the variable comp is assigned the values 10,20,30

Here we have to specify the maximum value that an array can hold inside a for loop or while loop while writing programs. This may sometimes result in exceptional errors. In order to avoid this Java provides us with length method. So far we have dealt with Single dimensional arrays. Java does not support multidimensional arrays. But we can use an array of arrays.

For example, let’s take a look at this

  1: int work[][] = new int [4][4];

means the variable work is assigned 4 rows and 4 columns respectively.

Inheritance in Java

So far we have dealt with single class programs. We will declare class <filename> and then write statements according to each program. The properties of each class is available to that class only. Inheritance refers to the properties of a class being available to other classes as well.

The original class is called as Base Class and Derived classes are classes created from the existing class ( Base Class ).It will have all the features of the base class. The concept of Inheritance is very important in Object oriented programming languages. It simplifies code writing thus making programs easier to maintain and debug.

Using Inheritance

Enter the following code into your Editor:

  1: class Book { 
  2: String name = "JAVA PROGRAMMING";
  3: int id = "34567";
  4: void show () {
  5: System.out.println( "Book name is " +name);
  6: System.out.println( "Book id is " +id);
  7: }
  8: }

Save this file as book.java and compile. The derived class is given below:

  1: class Book1 extends Book { 
  2: String author = "micheal janson";
  3: void show () {
  4: System.out.println( "Book name is " +name);
  5: System.out.println( "Book id is " +id);
  6: System.out.println( "The author name is " +author);
  7: }
  8: public static void main(String args[ ] ) {
  9: Book1 x = new Book1( );
 10: x.show();
 11: }
 12: }

Save this file as Book1.java. Compile and execute the derived file. You can see the three outputs. Note that the first two print statements in the derived class is inherited from book class (Base Class). We have not declared the variables in them in the derived class.

Methods in the derived class can have same name as that of the base class. If you are having one base class called book and two derived class called book1 and book2 and if you use same methods in all the three classes, then the last derived class method is executed although there are methods with similar names in all the former classes. This concept in Java is called as Method Overriding. If you want to display the details of base class and that of derived class in the above context, then super keyword is used in each derived classes after the method definition.

Dynamic Method Dispatch is used in Java. Normally, inside the main function in the derived class we will mention a reference (instance) to the derived class object. Java Supports a reference to base class object name and the derived class object name in a single code. For example we are having a base class object called circle and the derived class object rectangle extends base class object , circle. Inside main we have to specify circle z = new rectangle ( ); during runtime rectangle class method is executed.

Multiple Inheritance is not supported by Java. In order to balance this drawback, the creators of java have introduced the concept of Interfaces. An Interface consists of a set of method definitions. It contains the names of the methods with no body. Any class implementing the same should provide code for all its methods. You can implement any number of interfaces at a time in java.

Consider the code given below:

  1: public interface Address { 
  2: public void add(String add); 
  3: public void name(String name); 
  4: public void color(String ch); 
  5: }

The above code creates an interface called Address and it can be accessed by all classes. The code shown below gives you an idea about how to implement the above interface in a class:

  1: class A implements Address { 
  2: public void add(String add) { 
  3: System.out.println("Address" +add);
  4: }
  5: public void name(String name) { 
  6: System.out.println("Name" +name);
  7: }
  8: public void color(String ch) { 
  9: System.out.println("Color" +ch);
 10: } 
 11: public static void main(String args[]) { 
 12: A a = new A(); 
 13: a.add(“123,Park view,Chennai”); 
 14: a.name(“Swathi”); 
 15: a.color(“red”); 
 16: } 
 17: } 


Leave a Comment