Preface
This free book helps beginners to understand the reasons behind various coding constructs. And this book does not focus on any programming language. So, you can think it is for java or C# or whatever…(Examples were given in pseudo programming language)
Please sorry for grammar mistakes, English is not my native language. You’re welcome to point out anything I can improve on. Thank you!
R.Hasaranga
ruchira66@gmail.com
Index
- Expressions
- Statements
- Variables
- If Conditions
- Loops
- Functions
- Recursion
- Classes & Objects
- Static vs. Instance
- Constructor
- Encapsulation
- this keyword
- Inheritance
- Protected Modifier
- Overloading
- References
- Overriding & Polymorphism
- Abstract Classes & Interfaces
- Interfaces as callbacks
- Importance of abstraction
Statements
Statements are commands in your program which instructs cpu to do something. Statements are executed in sequential order (one after another). Following is an example:
Variables
Suppose we want to print the value of 1024*1024 twice.
The simplest form of our code would be something like this:
The problem of this code is we have to calculate the value of 1024*1024 two times. Instead of multiplying two times, we can use a temporary memory to store the calculated value. So, when we require the value of 1024*1024 again, we use that temporary memory instead of multiplying those values again. We named that temporary memory as a Variable.
Variables allow us to capture & reuse the result of calculation.
When you declare a variable, you’ll need to give a type of that variable first. Type of a variable identifies what it can hold. For example: int type can hold a number, string type can hold a name of a person
After you declare the type, you need to specify the name of the variable. This name can be use anywhere in your program where you want to use the value it holds.
Here is an example:
If Conditions
Sometimes we want our program to take decisions by reading external inputs. Without decisions, our program acts in same way for any input.
To make decisions we use if, else if, else control structures.
You can use following coding formats:
Following program reads an input from user & prints some details according to the value of that input.
Loops
Suppose you want to print “hello” 10 times. Will you write print “hello” ten times? nop.
That’s why we have loops! When we want to do the same thing again & again, we are using loops to do that.
Functions
Imagine you need to write a program which reads birth day from a user & print his age. You may write something like this:
Suppose now you need to read another user birth day & print his age.
You may write something like this:
And later you found something wrong with blah blah code section. So you will correct it. Since you have used same code twice, you will need to fix that error twice. If you have used ten times that code, you’ll have to fix that error ten times. That is so much waste of time. That’s why we have functions.
Functions/Procedures allow us to use same calculation for different values.
So, if we have used blah blah code section within a function, we don’t need to fix the same code multiple times.
When we declare a function, first we need to specify the return type. Return type specifies the type of output value of that function. For example: If we return a number, return type is int.
Then we need to specify the name of that function. After that we specify parameter/argument list of that function. Each parameter is separated by a comma.
Here is an example:
This is how we use that function within our program:
As you can see, Instead of using blah blah code again & again, we call CalculateAge function. This simplifies our code & improves reusability. Also other programmers can use that function without thinking about its implementation (blah blah code section). So, they can focus on their own coding problems instead of thinking about blah blah code section.
(Also you can declare variables inside of the function. We called them as local variables.)
Recursion
What if you call a function within itself? Well, that will make an infinite loop unless you make a break condition. Such behavior can be use to create another kind of loop called recursion.
Here is an example:
If you call the factorial function with number 4 as a parameter:
factorial ( 4 ) factorial ( 4 ) will expands to 4 * factorial ( 4 – 1 )
4 * factorial ( 4 – 1 ) factorial ( 4 – 1 ) will expands to 3 * factorial ( 3 – 1 )
4 * 3 * factorial ( 3 – 1 ) factorial ( 3 – 1 ) will expands to 2 * factorial ( 2 – 1 )
4 * 3 * 2 * factorial ( 2 – 1) factorial ( 2 – 1 ) will expands to 1 (break condition)
4 * 3 * 2 * 1
Final value is 4 * 3 * 2 * 1 which is 24.
Same behavior can be created using a loop:
Note: Thinking in recursion instead of loops will expand your brain. But it will break your stack at some point!
Classes & Objects
Suppose you want to make a function which holds some kind of state.
For example:
If you call buttonPressed function once it will print “on”. If you call it again it will print “off”. As you can see this function holds/manage some kind of state. In other words, its behavior is influenced by its history. The output of this function is depends on previous call of this function. Also we can say output of this function is depends on the time we call this function. Well… now our code feels the time… hmm… (There is no concept of time without history)
Imagine above code used to handle button press event of a window & now you want to add another button. As you can see you cannot use the same function to handle new button press event. So you will have to add another function & global variable for that new button.
Now our code will be something like this:
That’s ok for two buttons. But what if we want to handle 10 buttons?
So there should be some kind of way to use first example code as a template to create others. In other words, once we define global variable & function, there should be a way to create copies of it. In that case, we don’t need to create one global variable & function per button.
To use first code example as a template, we put it into a class.
Then we can use that class to create copies of it. Each copy of this class is named as object/instance of that class. The process of creating new object is named as instantiation.
The state of one object does not affect the other. They are independent. So if you change the state of one object, it will not impact on others.
If you look at the class definition code, we have named our class as Button. By declaring a class, we also introduce a new type into our programming language. We can define variables using this new type. We named those variables as references.
In this code, x is a variable and y is a reference.
Important: variable holds a value and reference holds an object.
To create a new object of class, we use new command with class name.
To capture the created object, we use a reference.
Now reference y holds an object of Button class. Let’s create another object of that class.
Now y and z holds two different objects whose type is same. Changing state of y does not affect the state of z.
The content of the class is named as members. As you can see there are two types of things we can put into a class. They are variables & functions. If we put variable into a class, we named it as a Field. If we put a function into a class, we named it as a Method.
According to the Button class definition, buttonPressed is a method & state is a field.
To access members of an object, we use the dot character.
Calling a method of an object is also called Invoking a method. When we consider class internals, method invocation of an object accomplish by sending a message into that object.
Let’s move into our Button example. Since we have a class, we don’t need to create one global variable & function per button. Instead, we create one reference & object per button. That’s easy…