Skip to main content

Reviewing the BASICS

Right knowledge in OOP makes your code and implementation more reliable. This can be achieved by correct coding practice and methodology. However, there will come a time that you’ll be affront with confusion of concepts and implementation of what OOP objects to use in right and efficient way. You’ll be stuck if you’re not well equipped. In this article, I’m hoping to aid newbies in this context, familiarize you with the basics of OOP that i think you need to know to get all throughout a smooth coding spree.


Here are some common discussions found in development forums that you might find useful resource.


Static vs Non-Static


Static members : Act like global variables. There is only one instance of it in the entire program execution. They can be used with the class name directly and shared by all objects of a class.


Example on referring a static method or data member.




class A {

static int firstInt;
static int secondInt;

static void initialize(int fnum, int snum) {
firstInt = fnum;
secondInt = snum;
}

static void ThisIsStaticMethod() {
/* first sample access */
System.out.println(\"First Num Prints .... \" + A.fnum);
/* second sample access, where we omitted the class name */
System.out.println(\"Second Num Prints .... \" + snum);

}

public static void main(String argv[]) {
/* \"A.\" is omitted with the same result */
initialize(55, 23);

/* \"A.\" could alse be omitted in this context */
A.ThisIsStaticMethod();

}

}

Non-Static members : Are defined for each object. There is a copy of each of these members for each object instantiated.




class A {

static int firstInt;
static int secondInt;

/* Class constructor */

A(int fnum, int snum) {
firstInt = fnum;
secondInt = snum;
}

void ThisIsAMethod() {

/* first sample access */
System.out.println(\"First Num Prints .... \" + this.fnum);
/* second sample access, where we omitted the prefix \"this\" */
System.out.println(\"Second Num Prints .... \" + snum);

}

public static void main(String argv[]) {

/* creating new instance of class A */
A newA = new A(55, 23);

/* invoking a method through the instance of newA */
newA.ThisIsAMethod();

}

}

Overloading vs Overriding


Overloading : Defining same method name with different arguments may or may not have same return type written in the same class itself.




void myTestMethod(int number){
System.out.println(number);
}

int myTestMethodReturn(int number, int snumber){

return number * snumber;
}

Overriding : The methods with the same name and same number of arguments and return type, implemented differently. One is in parent class and the other is in subclass.




class A {
public void myTestMethod(int number){
System.out.println(\"Simply prints : \" + number);
}
}

class B extends A {
void myTestMethod(int number){
super.myTestMethod(14);
System.out.println(\"This Prints : \" + number * 14);
}
}

Abstract class vs Interface


Interface : Contains abstract methods and properties yet without implementation . It usually defines the characteristic of class but not its identity or how it is being implemented merely stating the “can-do” of a class.


- A class may implement several interfaces.
- When defining an interface, putting abstract and public keyword is not necessary as all the methods and properties defined in interface are by default public and abstract.


Abstract class : Provides more structure. It usually defines some default implementations and has other member like a normal class has.


- A class can extend only one abstract class.


When to use



  • Use Interface when objects share common method signatures yet must be implemented differently.

  • Use Abstract when objects implementation are all of a kind and share a common status and behavior.


Access Modifiers



  • private Makes a method or a variable accessible only from within its own class.

  • protected Makes a method or a variable accessible only to classes in the same package or subclasses of the class.

  • public Makes a class, method, or variable accessible from any other clas.


Class, Method, and Variable Modifiers



  • abstract Used to declare a class that cannot be instantiated, or a method that must be implemented by a nonabstract subclass.

  • class Keyword used to specify a class.

  • extends Used to indicate the superclass that a subclass is extending.

  • final Makes it impossible to extend a class, override a method, or reinitialize a variable.

  • implements Used to indicate the interfaces that a class will implement.

  • interface Keyword used to specify an interface.

  • native Indicates a method is written in a platform-dependent language, such as C.

  • new Used to instantiate an object by invoking the constructor.

  • static Makes a method or a variable belong to a class as opposed to an instance.

  • strictfp Used in front of a method or class to indicate that floating-point numbers will follow FP-strict rules in all expressions.

  • synchronized Indicates that a method can be accessed by only one thread at a time.

  • transient Prevents fields from ever being serialized. Transient fields are always skipped when objects are serialized.

  • volatile Indicates a variable may change out of sync because it is used in threads.

Comments

Popular posts from this blog

Cross-Site Scripting - (HACK) a way out

I came across to this discovery when i was affronted with the problem of trying to communicate TWO sites on different domain/server and some parameters need to be passed. As you may have “Googled” it, you can’t do outright javaScript function call from your site to a partner site since it resides on different domains. It’s a violation of the Cross-Site Scripting W3C standards for it is highly probable for site security breach. To make the picture clearer, here’s the scenario. Take for an example your site caters a hotel reservation and you have a partner seller that also maintains a site for marketing. If you want to maximize you potential sales, you’ll opt to let your partner embed/include your reservation site somewhere in their site. See the diagram now? Partner site e.g resides in www.marketing.com and your site in www.hotelreservation.com , without directly accessing your site, a customer must be able to get a hotel reservation on you part...

Oracle Tips

Here are some helpful tips to remember when dealing with oracle. I. Use the “flashback technology” when you accidentally commit a mistake with your production data. (altering entire table contents, corrupted table data, or worst dropping unintended table). - First thing to do is to enable flashback on your database. ALTER DATABASE FLASHBACK ON; - Restoring database to its good state. FLASHBACK DATABASE TO RESTORE POINT bef_damage; - Restoring dropped table. FLASHBACK TABLE [TABLE_NAME] TO BEFORE DROP; - Restoring table to its good state. FLASHBACK TABLE [TABLE_NAME] TO TIMESTAMP TO_TIMESTAMP('[DATE_TIME]'); II. Manipulate date and time display Aside from to_date and to_timestamp functions , you could also alter the date and time display in your database through the use of this code below. ALTER SESSION SET NLS_DATE_FORMAT = '[DATE_FORMAT]'

Creating Bottom-up Web Service (WSDL)

This post will primarily show you how to create a simple Web Service application through Apache Axis in Eclipse , and will not dwell on explaining the background or functionality of a Web Service. Yet, it’s a de facto to at least give a little definition. WSDL or the Web Services Definition Language is just another specification to describe network XML-based services. It supports message-oriented and procedural approach XML technologies. (for further reading click here ) 1. Preparing the web application a. Create a new web application and name it as “SimpleWebService”. b. Download and add “axis.jar” ( download here ) to the application libraries. c. Edit and add this following configurations to the web.xml file. AxisServlet org.apache.axis.transport.http.AxisServlet AdminServlet org.apache.axis.transport.http.AdminServlet 100 AxisServlet /servlet/AxisServlet AxisServlet *.jws AxisServlet /services/* *Note: spa...