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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">int</span> array = <span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span> [20];

means array is assigned 20 memory locations or

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">int</span> 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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">int</span> work[][] = <span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span> [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.

READ  Learn how to activate a Button control using Java

Using Inheritance

Enter the following code into your Editor:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Book { 
  2: String name = &quot;<span style="color: #8b0000">JAVA PROGRAMMING</span>&quot;;
  3: <span style="color: #0000ff">int</span> id = &quot;<span style="color: #8b0000">34567</span>&quot;;
  4: <span style="color: #0000ff">void</span> show () {
  5: System.out.println( &quot;<span style="color: #8b0000">Book name is </span>&quot; +name);
  6: System.out.println( &quot;<span style="color: #8b0000">Book id is </span>&quot; +id);
  7: }
  8: }

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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Book1 <span style="color: #0000ff">extends</span> Book { 
  2: String author = &quot;<span style="color: #8b0000">micheal janson</span>&quot;;
  3: <span style="color: #0000ff">void</span> show () {
  4: System.out.println( &quot;<span style="color: #8b0000">Book name is </span>&quot; +name);
  5: System.out.println( &quot;<span style="color: #8b0000">Book id is </span>&quot; +id);
  6: System.out.println( &quot;<span style="color: #8b0000">The author name is </span>&quot; +author);
  7: }
  8: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[ ] ) {
  9: Book1 x = <span style="color: #0000ff">new</span> 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.

READ  Microsoft Announces New Azure Certifications

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:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> Address { 
  2: <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> add(String add); 
  3: <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> name(String name); 
  4: <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> 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:

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> A <span style="color: #0000ff">implements</span> Address { 
  2: <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> add(String add) { 
  3: System.out.println(&quot;<span style="color: #8b0000">Address</span>&quot; +add);
  4: }
  5: <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> name(String name) { 
  6: System.out.println(&quot;<span style="color: #8b0000">Name</span>&quot; +name);
  7: }
  8: <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> color(String ch) { 
  9: System.out.println(&quot;<span style="color: #8b0000">Color</span>&quot; +ch);
 10: } 
 11: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[]) { 
 12: A a = <span style="color: #0000ff">new</span> A(); 
 13: a.add(“123,Park view,Chennai”); 
 14: a.name(“Swathi”); 
 15: a.color(“red”); 
 16: } 
 17: } 

3 Responses to "Undertanding Arrays & Inheritance using Java"

  1. David Peers   May 15, 2010 at 2:29 pm

    This blog aided me somewhat in narrowing down some issues with the latest version, Why do they often seem to leave out vital documentation when they release a new version? It may be minor to them but not to me. I’m sure i’m not alone.

    Reply
  2. David Peers   May 15, 2010 at 4:27 pm

    This blog aided me in solving some issues with the latest version, Why do they often leave out vital documentation when they release a new version? It may be minor to them but not for us! I’m sure i’m not alone either.

    Reply
  3. Replica Scarf   September 8, 2010 at 4:18 am

    good this post,good is very good Puarly blog i have like it so much thanks friend for this fantastic blog Get the best network support for your business process online

    Reply

Leave a Reply

Your email address will not be published.