Saturday, November 1, 2014

OOP

About Reference Counter and Garbage Collector

Each object contains reference counter, contains the number of references which point to it.
Garbage collector passes by all objects reference counters from time to time, and removes any  not referenced objects.
You may give a hint to grabage collector to start working using System.gc()

About Eclipse IDE

To rename a field: right click on the field > refactor, so all the occurrences will be renamed
Auto format (Indent, spaces, ..etc): Right ht Click > Source > Format (ctrl+shift+f)
Automatically create setters and getters for an attribute: Right Click > refactor > create setters and getters

Basics

- Each Java program shall have one and only one “public static void main” as an entry point for the whole program
- package  mypack              // put the classes in this file in the package mypack
- import  package1.*         // import all classes of the package package1 to my file
- import  package1.class1                // import class1 of the package package1 to my file

Overriding Vs Overloading

- Overloading is defining more that one method with the same name (different arguments)
- Overriding:
You can define the same method with the same signature once in the super-class, and once in the subclass, each with different implementation.
In the subclass use “@Override”

Inheritance

  • Any class is a subclass of a super-class called “Object”.
  • If I have “Vehicle” super-class attributes: “Direction” and “speed”. And method “MoveForward()” “getSpeed”, Sub class “Truck” with attribute “Load” methods: “@override MoveForward()” and “getLoad”, “setLoad”
    • Vehicle car = new car();
    • Car.moveForward() // calls moveForward of the superclass
    • Vehicle otherCar = new Truck();
    • Car.moveForward()// calls moveForward of the truck subclass
    • Car.setLoad() // however the object of the class truck, and contains the method setload, but this method cannot be called from car reference
    • Instead, you can access this method by casting the reference “Car” to “Truck” as follows:
Truck otherTruck = (Truck) otherCar;
otherTruck.setLoad();
    • Any subclass constructor calls the superclass constructor
  • if (objName instanceof ClassName) // return true if objName references an object of type ClassName
  • Vehicle otherCar = new Truck();
  • new Truck() is a constructor method that
    • creates an object of class Truck in the memory
    • return a reference to this object
  • otherCar is a reference points to the created truck object.
  • It can only access methods defined in class Vehicle. (in case of overriding the method implementation of the subclass is used, because this is the one defined in the actual object created in the memory)
  • To access the fields defined in Truck -> cast this otherCar reference into reference to Truck.

Abstract Class

  • abstract class contain at least one abstract method.
  • abstract method prototype is declared in the abstract class
  • abstract method must be implemented in each subclass. (use @Override)

Interface

  • interface contains only unimplemented methods.
  • Classes that implements this interface must implement all the functions declared in the interface (use “@Override”)

LinkedList Vs ArrayList

 ArrayList: all elements in the same region in the memory à faster in looping for the array elements
 LinkedList: elements are not in the same memory region à faster if you will add and remove elements frequently

Strings concatenation

 Use “StringBuffer.append” instead of “+” while concatenation. à much better in time and memory utilization.
  • “We” + “ Want” + “ Freedom”
    • Write “We” in memory
    • Write “Want” memory
    • Gather we and want and put them in new place in memory “We Want” …etc

No comments:

Post a Comment