Object-Oriented Programming

This is a cheatsheet to brush up before attending interview.

Object: State+ Behavior.

Let’s take an example of real world object.  I see a Table Fan  infront of me 🙂

It has a state: On or Off, currentSpeed, Timer

These states can be changed through behaviors like turnOn, turnOff etc.

Software object also has state stored in “field” and behavior exposed using “methods”.

Data Encapsulation: The internal state of the objects are hidden. Methods are the only communication medium to interact with the state of an object.

Information hiding: The internal implementation details of an object remain hidden from outside world due to interactions enabled through object methods.

Class: It’s a blueprint from which objects are created.

In real world we see many objects being similar. They may share same blueprint.


Class TableFan{
   String state = "Off";
   int speed =0;
   static final MAX_SPEED=4;

   void turnOn(){
     state = "On";
   }

   void turnOff(){
     state = "Off";
   }

   void increaseSpeed(){
      if(speed<MAX_SPEED){
         speed = speed+1;
   }

   void decreaseSpeed(){
      if(speed>0)
         speed = speed-1;
   }

}

Inheritance: Different objects have certain amount of similar capabilities. OO-programming allows classes to inherit and refine common state and behavior from other classes. Java supports each class to have one direct superclass and a class can have multiple subclass.

Eg:


Class Circle extends Shape{
// Inherits from Shape class.
// Modify Shape class methods if necessary.
// Adds fields/methods particular to Circle class
}
Advertisements

Exporting data from Amazon RDS MySQL database to .csv or viewable in Excel sheet.

Amazon RDS do not allow to export table content to a csv file using mysql command.

select * from desired_table
INTO outfile "/path/to/csv file/file.csv"
fields terminated by ',';

gives an error: “Error Code:1045 Access denied for user ***@***.rds.amazonaws.com using password=YES”

As it said access denied I tried to grant access using:
grant all privileges
on YOUR_DATABASE.*
to 'username'@'***.rds.amazonaws.com'
identified by 'your_password';
>flush privileges;

But this did not work too.

There’s no issue from your end. MySQL creates the file on database server. It does not give permission to the client to create a file.

So a simple way to export the data to csv is by using unix sed command.
mysql -u username -p database_name -h ***.rds.amazonaws.com --port=3306 --batch -e "select * from desired_table"  |  sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv