Site icon Youth Ki Awaaz

Introduction to Java Part 6

mobile application developer offingapp

Introduction

Object Oriented Programming is a complicated term for an elegant way of describing what a computer program is and how it logistics app development technology works.

 

 

We will cover:

 

How Object Oriented Programming Works

Each Java program that you have written or will write can be thought of as an object, like anything else in the real world, such as a car, bike, fridge or people. People consist of a head ( I have known some with more than one but I won’t go into that), two arms, two legs, two hands a torso etc. Each part of the body performs specific tasks and has things that make it different from the rest of the body. If you break down computer programs the same way as you have done with a persons body then you are performing Object Oriented Programming or OOP.

An object contains two things, attributes and behavior. Attributes describe the object and the behavior describes what the object does.

With Java we create an object by using a class as a template. The class is a master copy of the object that is consulted to determine which attributes and behavior an object should have. Every Java program that you create will be a class because each one has used a template for the creation of new objects.

Consider the PC that you are using at the moment, describe the PC in detail, its’ case, power supply, hard disk(s), motherboard, modem, Graphics Card, Monitor. They are all objects that combine to make your PC. Each object exist independently of the other. The modem object does its’ job without requiring any help from the monitor object.

What Objects Are

Objects are created by using a class of objects as a guideline or template. The following is a class.

public class dog
{
}

Any object created from this class can’t do anything because it doesn’t have any attributes or behavior.

public class dog
{
String name;
public void speak()
{
System.out.println(“Woof Woof”);
}
}

The dog class now looks like the programs we have already written except that it has a public statement alongside it. The public statement means that the class is available for use by the public – in other words, by any programs that want to use the dog object.

The first part of the dog class creates a string variable called name. This variable is an attribute of the object, this is what distinguishes one dog from another. The second part of the dog class is a method or behavior called speak(). This method has one statement, the System.out.println statement to display ‘Woof Woof’.

 

If you wanted to use a dog object in your program then you could you the following statement

dog myDog = new dog();

We can now set the name of myDog

myDog.name = “Barney”.

/* He was named after Barney Rubble, not a purple dinosaur */

To make Barney speak by calling the speak method we could use the following:

myDog.speak();

 

The computer will respond with “Woof Woof” unlike the real Barney.

The final selling point of Object Oriented Programming is inheritance. When you start creating objects for use in programs, you will find that some objects you want are a lot like other objects that have already been developed.

A class that inherits from another is called a subclass, and the class that is inherited from is called a superclass.

Creating an Object

public class mammal
{
String name;
public void sleep()
{
System.out.println(“zzzzzzzzzzzzzzzzzzz”);
}
}

Cut n’ paste or type this program into your editor.

Compile the code using javac mammal.java.

We now have a public class called mammal that has the attribute name and the method sleep() i.e. it snores.

public class dog extends mammal
{
public void speak()
{
System.out.println(“Woof Woof”);
}
}

Cut n’ paste or type this program into your editor.

Compile the code using javac dog.java.

We now have a subclass dog of the superclass mammal, dog has a attribute name that it has inherited from the superclass mammal and the methods sleep() and speak().

/* Java Lesson 6a */
class lesson6a
{
public static void main(String arguments[])
{
dog doggie = new dog();
doggie.name = “Barney”;
System.out.println(“Barney is Barking”);
doggie.speak();
System.out.println(“Barney is sleeping”);
doggie.sleep();
}
}

Cut n’ paste or type this program into your editor.

Compile the code using javac lesson6a.java.

When there are no errors, run the program using java lesson6a.

You should see the following output.

‘Barney is Barking’
‘Woof Woof’
‘Barney is sleeping’
‘zzzzzzzzzzzzzzzzzzz’

I hope you are all clear with Object Oriented Programming OOP and inheritance.

Exit mobile version