Wednesday, 24 April 2013

Linux Command Line Cheat Sheet

Bash Commands

 uname -a Show system and kernel
head -n1 /etc/issue Show distri­bution
mount Show mounted filesy­stems
date Show system date
uptime Show uptime
whoami Show your username
man command Show manual for command

Directory option

pwd Show current directory
mkdir dir Make directory dir
cd dir Change directory to dir
cd .. Go up a directory
ls List files

ls commands

-a Show all (including hidden)
-R Recursive list
-r Reverse order
-t Sort by last modified
-S Sort by file size
-l Long listing format
-1 One file per line
-m Comma-­sep­arated output
-Q Quoted output

Search Files

grep pattern files Search for pattern in files
grep -i Case insens­itive search
grep -r Recursive search
grep -v Inverted search
grep -o Show matched part of file only
find /dir/ -name name* Find files starting with name in dir
find /dir/ -user name Find files owned by name in dir
find /dir/ -mmin num Find files modifed less than num minutes ago in dir
whereis command Find binary / source / manual for command
locate file Find file (quick search of system index)

File operations

touch file1 Create file1
cat file1 file2 Concat­enate files and output
less file1 View and paginate file1
file file1 Get type of file1
cp file1 file2 Copy file1 to file2
mv file1 file2 Move file1 to file2
rm file1 Delete file1
head file1 Show first 10 lines of file1
tail file1 Show last 10 lines of file1
tail -f file1 Output last lines of file1 as it changes

Process management

ps Show snapshot of processes
top Show real time processes
kill pid Kill process with id pid
pkill name Kill process with name name
killall name Kill all processes with names beginning name

File Permissions

chmod 775 file Change mode of file to 775
chmod -R 600 folder Recurs­ively chmod folder to 600
chown user­:g­roup file Change file owner to user and group to group

Tuesday, 23 April 2013

Interfaces in Java

Having learned what are abstract classes and functions we are all set to dig a bit more into polymorphism. Interfaces are based on these two features.



We saw Java does not support multiple inheritance to avoid the problem of Deadly Diamond of Death. To provide functionalities that multiple inheritance support Java provides Interfaces. Interfaces are entities just like classes. We define them one per file and file name must be same as the interface name.

   Synatx for creating an interface is as follows -

    public interface interfaceName{ //code goes here}

Example of an interface is as follows -

public interface Animal{
    public void move();
}


Couple of things to note in Interfaces
  • All functions in an interface are public and  abstract by default. This means all interface contains is function prototype. It's concrete implementation will be in the class which implements it.Note the semicolon after the function. No implementation.
  • For sub class inheriting from super class we say classA extends classB whereas for interfaces we say classA implements interfaceB.
  • You can define variables in interfaces but they are final and static by default.
  • You cannot create objects of interfaces. You can think them as abstract classes for your understanding because after all it contains abstract functions and we know that non-abstract classes can't have abstract function. only abstract classes can.
 Note : If you have an abstract class will all abstract methods as per convention you should go for interface rather than abstract class (As per Java specs).
Let take a look at any class that may implement interface defined above.

 public class Dog implements Animal{
    public void move() {
        System.out.println("Moving..");
    }

    public static void main(String args[]) {
        Animal animal = new Dog();
        animal.move();
    }
}


output :  Moving..
as expected.


Note in above code we have made use of polymorphism. We are using a Animal reference to refer to a Dog object. All rules of polymorphism will follow.

Note :  We can use interface as polymorphic references in method arguments and return types. Meaning you can have a method that return Object of type Animal. At runtime you can return any class Object that implements Animal interface.


Why are the advantages of using interfaces?

  • A class can implement multiple interfaces but can extend only one class.
  • Interfaces can be used across inheritance trees. This means a class from one inheritance tree can implement interface from another inheritance tree.
  • Advantages of polymorphism follows.
  • In later parts of tutorials on Java you will realize generics , design pattens etc depend on this.

One of the OOP principle is "Code for the interface not for implementation" meaning always thing how to design your interface then to worry about your implementation.

Note :  If you have abstract class with all abstract methods (no non-abstract methods) it is better to go for an interface.

Sunday, 21 April 2013

Why do we use Linux?


We tell people we use Linux because it's secure or it's free or it's customizable or it has excellent community support.
     But all of this is just marketing bullshit. We tell that to non Linux users because they wouldn't understand the real reason and when we say those false reasons enough we might even start to believe them ourselves.

   But deep underneath , the real reason remain - We use Linux because it's fun! It's fun to tinker with your system. It's fun to change all the settings, break the system, then have to go back to recovery mode to fix it.It's fun to have a 100 distros to choose from.It's fun to use command line.
    Let me say that again - it's fun to use command line.No wonder non Linux users won't understand.

     The point with Linux fan is we use Linux for it's own sake.Sure, we like to get work done. Sure, we like to be secure from virus.Sure, we like to save money but these are only the side effects.What we really like is playing with the system, poking around and discovering fascinating facts about the software that lies underneath it.
 

Saturday, 20 April 2013

Interview Question #9 Why multiple inheritance is not supported by Java?

Multiple inheritance has a problem known as The Deadly Diamond of Death.

Refer to the following diagram to understand The Deadly Diamond of Death-


Explanation -  

     Suppose you have class C which extends two classes A and B.Lets say A and B both have a function with same prototype eg. public int getData(). Now if we create an object of class C and call the function i.e ObjC.getData() which function will be called? Decision is ambiguous unless we have set some rules and priorities. To avoid such ambiguity Java does not allow multiple inheritance.


A language that allows Deadly Diamond of Death can lead to some ugly complexities, because you have to have special rules to deal with the potential ambiguities.

Java is designed to be simple. Java (unlike C++) protects you from having to think about Deadly Death of Diamond.

To provide similar functionality Java provides interfaces.
So Java can implement two or more interfaces but can extends only one super class.

Friday, 19 April 2013

Abstract classes and functions in java

Abstract classes are the basis for what is yet to come - interfaces. So lets understand what are abstract classes and why are these important.

In the tutorial on polymorphism we saw how reference of type Animal can be used to point at object of various types like Cat,Dog,Lion,Wolf,Hippo etc. 
 
Just to revise things we now know we can say -

Wolf aWolf = new Wolf();


or we can also say -

Animal aHippo = new Hippo (); 


 but what happens when we do -

Animal animal = new Animal();

Perfectly Valid Java syntax. But what does this Animal look like?

This only makes sense when we have some concrete Animal like Dog,Cat etc . In short it does not make any sense to create objects of class Animal or in other words it does not  make any sense to use new() operator with Animal class. In this case we declare the class as abstract. Such abstract classes are used in polymorphism.

Syntax for making abstract classes
abstract public class Animal

Note: Though we cannot create objects of abstract classes we can use them as reference (like in Polymorphism).

Also remember you cannot use functions of subclass through super class reference(polymorphism) unless the function is defined in the super class. But also we cannot implement the function in the super class as the implementation will be subclass specific( eg different implementation for Dog,Cat etc). To sum thing up, for using polymorphism effectively we need function declared in super class and it's concrete implementation in the subclass. Hence we define the functions to be abstract.

Syntax for abstract function -
abstract returnType functionName(Arguments);

Note : No Method body. No brace brackets. Just a semicolon after method declaration. 


 Observe the semi colon after function prototype.  We just declare it and put a semi colon after it. Implementation of this function will be in the first concrete class which implements this abstract class. There is no use of abstract class if no class implement it(i.e if there are no subclasses of abstract class).

Note: There can be non-abstract functions in abstract classes but there can never be abstract functions in a non-abstract class. So if you have abstract functions you must declare your class to be abstract.

Also note there is no such thing as abstract variables. In java we just have abstract classes and abstract function.

Few points to note

  1. A non abstract class cannot have abstract method. If a class has abstract method it has to be made abstract.
  2. We cannot instantiate abstract classes.
  3. An abstract class can have non abstract methods .
  4. If your abstract class has all abstract method it is better to go for an interface instead.









t> UA-39527780-1 back to top