programming, technology

How to think in objects and classes – Java — Part 3


Welcome back to our OOP series. If you’re here then I assume you’ve taken a look at Part 1 and Part 2. Let’s continue to our topic for today, Inheritance.

Inheritance as defined by us
So let’s define inheritance in our own practical way. Inheritance means, we have two classes X and Y, class X has properties A and B,  class Y appears to have property C but it is derived from X so it has properties A and B by default.

Think of it as genetic inheritance. We have our unique personalities but we inherited some things from our predecessors. Imagine your dad has superpowers; he can fly. You can also fly but besides flying you can also live under water 😀 Cool huh! You’ve inherited the ability to fly but you have your own ability that your dad doesn’t.

Fig 1
person_diagram

Take a look at fig 1. Singer and Dancer are Person s and have the same attributes and methods (functions) but they can do more than a normal person can!

Inheritance helps us write DRY (don’t repeat yourself)  code. In actual sense, we do not declare the attributes and methods all over again. That is, the text in blue are there for clarity. They do not appear in the actual code.

Let’s code!

Ok so now, let’s look at some actual code and see how it is like in Java.

The Singer class

//Singer.java
package javaapplication1;

public class Singer extends Person{
private int career_age;

Singer(String firstname, String lastname,
String location, int age, int career_age){

super(firstname, lastname, location, age);
this.career_age = career_age;
}

public void sing(){
System.out.println("so - mi fa so - mi fa so - so la ti do "
+ "re mi fa mi - do re mi...");
}
}

You’re there looking at the code like “oh boy! what the heck is this”. Calm down, it’s no big deal at all. We’ll take it step by step ok.

The first thing to know is, we use the keyword extend to inherit from a parent class like it is up there. Once we do that, the current class now becomes a clone of the parent class, then we can go ahead and add more features to the current class.

You realize the constructor of our new class takes five arguments and not one. Also, we pass four of these arguments to a weird looking function we haven’t talked about, super .

What is happening here is, we take all the parameters of the parent class when we create objects and then call a special function super which then does what the constructor of the parent class will do when called. So in effect, it passes those parameters to the attributes inherited from the parent. The super method, must be the first to be called in a derived class.

We’ve also declared just one function but we still have access to the other functions in the parent class. We’ll demonstrate that in a minute.

The Dancer class


//Dancer.java
package javaapplication1;

public class Dancer extends Person{
private double height;

Dancer(String firstname, String lastname,
String location, int age, double height){

super(firstname, lastname, location, age);
this.height = height;
}

public void dance(){
System.out.println("robo dance");
System.out.println("left, up, down, right");
}
}

The parent (Person) class


package javaapplication1;

public class Person {
protected String first_name;
protected String surname;
protected String location;
protected int age;

public Person(String first_name, String surname, String location, int age){
this.first_name = first_name;
this.surname = surname;
this.location = location;
this.age = age;
}

public String say_my_name(){
return "My name is "+first_name+" "+surname+
". But you can just call me "+ first_name.substring(0,3);
}

public void walk(){
System.out.println("Walking...");
}

public void talk(String what_to_say){
System.out.println(first_name+" says: "+what_to_say);
}
}

Protected

As you can see, the private keyword in the Person class has been changed to protected. This allows classes that are inheriting to access the attributes of the parent class.

Let’s play

Now let’s go into our main method and see how objects from these classes interact.


//JavaApplication1.java
package javaapplication1;

public class JavaApplication1 {

public static void main(String[] args) {
Person ordinary_person = new Person("Benard","Bush","New York", 28);

//we've given person1 some details

Dancer dancer_person = new Dancer("Samuel","Sanders","Detroit",24, 1.92);
Singer singer_person = new Singer("Aba", "Addae","Ghana", 24, 10);

//let's walk and talk
ordinary_person.walk();
ordinary_person.talk("Hi there.. what are your names");

dancer_person.talk(dancer_person.say_my_name());
singer_person.talk(singer_person.say_my_name());
ordinary_person.talk("Great!");
ordinary_person.talk(ordinary_person.say_my_name());

ordinary_person.talk("So what can you do?");
dancer_person.talk("Watch me...");
dancer_person.dance();

singer_person.talk("Now listen to me");
singer_person.sing();
}
}

Output

Walking…
Benard says: Hi there.. what are your names
Samuel says: My name is Samuel Sanders. But you can just call me Sam
Aba says: My name is Aba Addae. But you can just call me Aba
Benard says: Great!
Benard says: My name is Benard Bush. But you can just call me Ben
Benard says: So what can you do?
Samuel says: Watch me…
robo dance
left, up, down, right
Aba says: Now listen to me
so – mi fa so – mi fa so – so la ti do re mi fa mi – do re mi…

[/code]

Make sure you run every code. It helps you understand better 🙂

Let me have your comments so that I can better adjust my style to help us all.

 

 

2 thoughts on “How to think in objects and classes – Java — Part 3”

Leave a comment