Saturday, September 26, 2015

Superclasses and Subclasses

Often, an object of one class is an object of another class as well. The figure  lists several



simple examples of superclasses and subclasses—superclasses tend to be “more general”

and subclasses “more specific.” For example, a CarLoan is a Loan as are HomeImprovement-

Loans and MortgageLoans. Thus, in Java, class CarLoan can be said to inherit from class

Loan. In this context, class Loan is a superclass and class CarLoan is a subclass. A CarLoan

is a specific type of Loan, but it’s incorrect to claim that every Loan is a CarLoan—the Loan



could be any type of loan.

 

Wednesday, September 23, 2015

Inheritance in java


 
 

inheritance, which is a form of software reuse in




which a new class is created by absorbing an existing class’s members and embellishing

them with new or modified capabilities. With inheritance, you can save time during program

development by basing new classes on existing proven and debugged high-quality

software. This also increases the likelihood that a system will be implemented and maintained

effectively.

When creating a class, rather than declaring completely new members, you can designate

that the new class should inherit the members of an existing class. The existing class

is called the superclass, and the newclass is the subclass. (The C++ programming language

refers to the superclass as the base class and the subclass as the derived class.) Each subclass




can become a superclass for future subclasses.

A subclass can add its own fields and methods. Therefore, a subclass is more specific




than its superclass and represents a more specialized group of objects. The subclass exhibits

the behaviors of its superclass and can modify those behaviors so that they operate appropriately

for the subclass. This is why inheritance is sometimes referred to as specialization.

The direct superclass is the superclass from which the subclass explicitly inherits. An

indirect superclass is any class above the direct superclass in the class hierarchy, which




defines the inheritance relationships between classes. In Java, the class hierarchy begins with

class Object (in package java.lang), which every class in Java directly or indirectly extends



(or “inherits from”).

Tuesday, September 22, 2015

Set and Get Methods vs. public Data

It would seem that providing set and get capabilities is essentially the same as making the

instance variables public. This is one of the subtleties that makes Java so desirable for software

engineering. A public instance variable can be read or written by any method that

has a reference to an object containing that variable.
 If an instance variable is declared private,

a public get method certainly allows other methods to access it, but the get method

can control how the client can access it. For example, a get method might control the format




of the data it returns and thus shield the client code from the actual data representation.

A public set method can—and should—carefully scrutinize attempts to modify the

variable’s value and throw an exception if necessary. For example, an attempt to set the day

of the month to 37 would be rejected, an attempt to set a person’s weight to a negative

value would be rejected, and so on. Thus, although set and get methods provide access to

private data, the access is restricted by the implementation of the methods. This helps



promote good software engineering...

Monday, September 21, 2015

Two dimensional array in java


 

public class Apples {



public static void main(String[] args){

int [][] array1 = {{1,2,3},{4,5,6}};

int [][] array2 = {{1,2},{3},{4,5,6}};

System.out.println("elements of array1 by row");

outputArray(array1);

System.out.println(" \n elements of array2 by row");

outputArray(array2);



}

public static void outputArray(int[][] array){

for(int row=0; row < array.length ; row++){

for(int column=0; column < array[row].length ; column++){

System.out.printf(" %d " , array[row][column]);



}

System.out.println();


}

}

}

Sunday, September 20, 2015

Example on Arrays in java


 

public class Apples {



public static void main(String[] args){

int[] array;

array = new int[10];

System.out.printf("%s%8s \n","inex" , "value" );   // putting headlines



for (int counter=0 ; counter < array.length ; counter ++)     // printing the array out



System.out.printf("%5d%8d \n" , counter , array[counter]);



}

}

Friday, September 18, 2015

Declaring and Creating Arrays in Java

Array objects occupy space in memory. Like other objects, arrays are created with keyword

new. To create an array object, you specify the type of the array elements and the number

of elements as part of an array-creation expression that uses keyword new. Such an expression



returns a reference that can be stored in an array variable. The following declaration

and array-creation expression create an array object containing 12 int elements and store

the array’s reference in array variable c:

int[] c = new int[ 12 ];

This expression can be used to create the array shown in Fig. 7.1. When an array is created,

each element of the array receives a default value—zero for the numeric primitive-type elements,
 
false for boolean elements and null for references. As you’ll soon see, you can



provide nondefault initial element values when you create an array.

Creating the array can also be performed in two steps as follows:
int[] c; // declare the array variable

c = new int[ 12 ]; // create the array; assign to array variable


Common Programming :
 
 
 
In an array declaration, specifying the number of elements in the square brackets of the
 
declaration (e.g., int[12] c;) is a syntax error.

Good Programming Practice :
 
 
 
For readability, declare only one variable per declaration. Keep each declaration on a separate

line, and include a comment describing the variable being declared

A program can declare arrays of any type. Every element of a primitive-type array contains

a value of the array’s declared element type. Similarly, in an array of a reference type,

every element is a reference to an object of the array’s declared element type. For example,
 
every element of an int array is an int value, and every element of a String array is a reference

to a String object.