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.
 
 

Wednesday, September 16, 2015

Arrays in java

An array is a group of variables (called elements or components) containing values that all

have the same type. Arrays are objects, so they’re considered reference types. As you’ll soon



see, what we typically think of as an array is actually a reference to an array object in memory.

The elements of an array can be either primitive types or reference types (including



arrays). To refer to a particular element in an array, we specify

the name of the reference to the array and the position number of the element in the array.

The position number of the element is called the element’s index or subscript.

This array contains
 
12 elements. A program refers to any one of these elements with an array-access

expression that includes the name of the array followed by the index of the particular element

in square brackets ([]). The first element in every array has index zero and is sometimes

called the zeroth element. Thus, the elements of array c are c[0], c[1], c[2] and so

on. The highest index in array c is 11, which is 1 less than 12—the number of elements



in the array. Array names follow the same conventions as other variable names.

 

Tuesday, September 15, 2015

Method Overloading in java

Methods of the same name can be declared in the same class, as long as they have different

sets of parameters (determined by the number, types and order of the parameters)—this

is called method overloading. When an overloaded method is called, the compiler selects



the appropriate method by examining the number, types and order of the arguments in

the call. Method overloading is commonly used to create several methods with the same

name that perform the same or similar tasks, but on different types or different numbers



of arguments. For example,

Declaring Overloaded Methods


 

public class Apples {



public static void main(String[] args){

System.out.printf("square of integer 7 is %d \n", square(7));

System.out.printf("square of double 7.5 is %f \n", square(7.5));



}

public static int square (int intValue){

return intValue*intValue;


}

public static double square (double doubleValue){

return doubleValue*doubleValue;


}

}
 

Monday, September 14, 2015

Java API Packages in java

Java API Packages or Java Application programming Interface

As you’ve seen, Java contains many predefined classes that are grouped into categories of

related classes called packages. Together, these are known as the Java Application Programming

Interface (Java API), or the Java class library. A great strength of Java is the Java

API’s thousands of classes. Some key Java API packages are described in Fig. 6.5, which

represents only a small portion of the reusable components in the Java API.
 
Package Description
 
 
java.applet  ,The Java Applet Package contains a class and several interfaces required



to create Java applets—programs that execute in web browsers. Applets

 
java.awt ,  The Java Abstract Window Toolkit Package contains the classes and



interfaces required to create and manipulate GUIs in early versions of
 
Java. In current versions, the Swing GUI components of the javax.swing

packages are typically used instead.
 
 
 
 
java.awt.event , The Java Abstract Window Toolkit Event Package contains classes and



interfaces that enable event handling for GUI components in both the
 
java.awt and javax.swing packages



 
java.awt.geom ,The Java 2D Shapes Package contains classes and interfaces for working



with Java’s advanced two-dimensional graphics capabilities.
 
java.io , The Java Input/Output Package contains classes and interfaces that



enable programs to input and output data.
 
java.lang ,The Java Language Package contains classes and interfaces (discussed



bookwide) that are required by many Java programs. This package is

imported by the compiler into all programs.
 
java.net ,The Java Networking Package contains classes and interfaces that



enable programs to communicate via computer networks like the Internet.

 
java.sql  ,The JDBC Package contains classes and interfaces for working with



databases.
 
java.utilThe Java Utilities Package contains utility classes and interfaces that



enable such actions as date and time manipulations, random-number
 
processing (class Random) and the storing and processing of large



amounts of data.
 
java.util.concurrent The Java Concurrency Package contains utility classes and interfaces
 
 




for implementing programs that can perform multiple tasks in parallel.
 
javax.media ,The Java Media Framework Package contains classes and interfaces for



working with Java’s multimedia capabilities.
 
javax.swingThe Java Swing GUI Components Package contains classes and interfaces



for Java’s Swing GUI components that provide support for portable

GUIs.projavax.
swing.event , The Java Swing Event Package contains classes and interfaces that
 
 
enable event handling (e.g., responding to button clicks) for GUI components
 
in package javax.swing.
 
 
 
 
 
javax.xml.ws , The JAX-WS Package contains classes and interfaces for working with
 
 
web services in Java.