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

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...

How to get rid of VB Script Just-In-Time Debugger Error

Lately i have been pestered with a lame error every time my Windows starts up. The “VB Script Just-In-Time Debugger Error” shows up and it would terminate the explorer.exe process upon clicking OK. Somehow something went wrong in the system but the error does not specifically says what it is. I don’t have a clue how to resolve it. We all know explorer.exe is critical for all windows to work, thus leaving me no choice but to run it manually. For normal users who do not know how to run the explorer.exe manually, they will be paralyzed. They won’t find their way to work it except to ask for help, which sometimes can be so annoying specially when you’re up to finish a deadline. Luckily, i was so persistent enough to search for a solution. Though no one gave the exact process of eliminating this error, I come up to finally solve it through my compilation of readings and i’ll share it with you. Here's how to get rid of this error: 1. Open Regedi...

Search Engine Optimization (SEO)

Often we focus our site development mainly on its GUI (Graphical User Interface) and content, although this is very critical and needed, yet we tend to neglect adding some essentials that also need to be considered and included in order to optimize the site search ability. We want surfers to find and read our sites especially if we want to market something, thus it is important to make sure that our site link will be included in the list of search results on any search engines. The question is how will you do it? How will Google, Yahoo, msn etc, will find your site be indexed and included in their search results? There are a lot of things to learn and take into consideration if you want to optimize your site. Before proceeding, I’ll provide primary reference for you to get started before jumping into SEO. Read through this and decide if you need to reconstruct your site or not. Google Webmaster Site – This will acquaint you to search engines basic concepts. Webmaster Guidel...