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
Post a Comment