Thursday, December 4, 2014

Dive into PIE - 'I', Part - 1

Now, at this point we can deal with the business logic and aware of how to implement them using Java. So using Java, you can write your own program to deal with things you want to.

So, I've learnt Java ?
Not really, you have just learnt the most basic part of it. Java is so vast, that it can take you your whole lifetime to learn it completely.

Ohh...(Aww...) So, what's next ?
Well, as we've learnt the basic things, basic terminologies and basic operations, let's take a deeper look at PIE.

At this point, I want to start with each of the terminology separately. Let's start with widely used I.

I stands for Inheritance in OOP.

Inheritance, don't you think that is the property of a live creature ?
What to do with that in Java ?
Well, you are right, Inheritance is the nature of live objects like people inherit behaviour from their parents. In the world of OOP as well, some object can inherit behaviour from other object.

So, what does it inherit ?
Child class can inherit fields and methods of parent class.

What is Child ?
Well child class is the class which inherits another class.

What is Parent ?
The class which is being inherited in the child class is known as  parent class.

Well, its a bit confusing now, right ?

OK, let me go through an example.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Jun 29, 2015
 */
class Parent {
 String name = "Palash";

 public String getName() {
  return this.name;
 }
}

public class Child extends Parent {
 public static void main(String[] args) {
  Child child = new Child();
  System.out.println(child.getName());
 }
}

When you run the code, you will get to see,


Palash

Oh I see, but I am not getting it, what you are trying to achieve through it.

Go through the code again.

Did you note anything ?

Where is getName() in Child class ?
Hmm, there is no such method in Child class. This one is written in Parent class.

So, how does the program run ?
Well, getName() is written in parent class. You may have ignored the extends keyword.

Oh, Yes, there is something like that in the class definition. What is that for ?
When you use this keyword, the class inherits the allowed fields and methods in its own. This basically promoted code re-usability.

Now, let me give you a diagrammatic example,
Inheriting properties
So, as we can see, Person class has fields Name, Age and method Eat(), this class is being extended in Student class which has its own method attendClass().

So, in Student class, we have fields Name, Age, Class, Roll_No. and methods Eat() and attendClass().

Well, so Java_Students has fields  Name, Age, Class, Roll_No. and methods Eat(), attendClass() and writeJavaProgram(). Am I right ?
Exactly.

In the next post, we'll take a deeper look on Inheritance, what you can do and what not etc.


Prev   Next
Palash Kanti Kundu

No comments:

Post a Comment