Show Menu
Cheatography

OOP implementation in JAVA Cheat Sheet by

This cheat sheet is a summary of the OOP course taught in INSAT to 2nd year students who chose the Software Engineering branch

OOP goals

Simple modeling
An accurate repres­ent­ation of the real world by grouping objects with their properties and actions
Robustness
Easy mainte­nance and bug detection. Strong typing which results in more robust code (predi­ctable behavior of your code)
Scalab­ility
Adding functi­onality comes down to establ­ishing new connec­tions with other objects and methods.
Reusab­ility
Available features allowing code reusab­ility. (inher­itance)

OOP building blocks

Class
A blueprint of an object: its proper­ties, behavior and how it interacts with the exterior world.
Attributes & methods
The attributes are the properties of an object while the methods represents its dynamic behavior inside of your program. These attributes and methods define how the object should be accessed, its internal behavior and how it interacts other objects
Objects
An instance of a class. (the class is the type definition and the object is the variable) => all instances of a class share the same fields but have distinct data inside.
In addition to its state (prope­rties) and behavior (methods) an object has an identity which distincts it from other objects (alias/ memory address ...)

OOP Paradigms

Abstra­ction
The selection of only useful inform­ation about an object for a particular applic­ation.
Encaps­ulation
Is the ability to control the access to the object's properties and methods, render them either visible (public) or hidden (visible only to internal functi­ons).
Polymo­rphisme
The definition of different executions of a methode for different inputs and for different objects
Heritage
A descri­ption of a genera­l\s­pecific relati­onship between classes. In shorts a subclass has all the proper­ties/ actions of its super class + its own ones + its redefined ones
Compos­ition, Agregation
The creation of a class/­object as a collection of other objects
 

JAVA general infos

JAVA platform = JVM + API JAVA
JVM : execution enviro­nment for JAVA apps.
Allows code to be machine indepe­ndant as it executes inside of a virtual machine which abstracts the specefics of input­\output, hardware config­ura­tion, and different OSs.
JVM has its own native language : byte code (juts like a real computer has its own instru­ction set)
The JVM interprets byte code and manages memeory for the programs automa­tically by its Garbage collector
JAVA API : libraries that abstracts diverse functi­ona­lities.

JAVA main function (entry point)

public class test{
public static void main(String ar[]){
// code
}
}

Array declar­ation

In java you must allocate the memory for array frst by
type [] name=new type[size]
or
type name[] = new type[size]

Then if the type is not primitive you need to instan­tiate each field of the array by
array_­nam­e[i­]=new type(c­ons­tructor attrib­utes)

Also, we can declare arrays like this
type array_­nam­e[]­={v­al1­,va­l2,­val3};

Strings

Declar­ation:
String s="s­ample text";
String s=new String­("sample text")

Some string methods:
s.equa­ls(s2);
returns 1 is s=s2 and 0 otherwise
s3=s1.c­on­cat­(s2);
<=>
s3=s1+s2;
to concat­enate strings

I/O

INPUT
in the header of the class file :
import java.u­til.Sc­anner;

in the method:
Scanner sc = new Scanne­r(S­yst­em.in);

int i = sc.nextInt();
double d = sc.nextDouble();
long l = sc.nextLong();
byte b = sc.nex­tBy­te(); // etc
OUTPUT
for single variables:
System.ou­t.p­rin­tln­(single variable);

for multiple variables:
System.ou­t.p­rin­tln­(va­r1+­" "­+va­r2+...);

the idea is to convert variables to strings and concat­enate them
 

Garbage Collection

//The garbage collector deallocates memory of unreferenced objects
//example
cl obj1=new cl();
System.gc();
//the GC does nothing here since obj 1 is still referenced
cl=null;
System.gc();
//the GC deallocates the memory previously allocated for obj1 since it's no longer referenced
//This makes the life of a programmer (for example when freeing an array of objects : a single command instead of a loop

Properties of static class members

Static attributes are initia­lized as follows: ints, floats -> 0, bools -> false, references -> null
static methods have access only to static methods and attributes of a class (obvio­usl­y).They can't have use the "this" reference since it doesn't make any sense.
Class_­nam­e.s­tat­ic_­atr­rib­ute­_name

Class_­nam­e.s­tat­ic_­met­hod­_name()

Class members visibility

   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Java + OOP concept Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet

          More Cheat Sheets by mahdi007

          Système d'exploitation Cheat Sheet