Object Oriented Approach of Java Programming and Encapsulation – Part 5

Since the beginning of this series (and even before that) you knew Java is an Object Oriented Programming Language. The object oriented Programming Language is based upon the concept of “objects”, which contains data...

Spread the love

Since the beginning of this series (and even before that) you knew Java is an Object Oriented Programming Language. The object oriented Programming Language is based upon the concept of “objects”, which contains data as attributes in methods.

Object Oriented Approach of Java

Object Oriented Approach of Java – Part 5

Every object in Java has state and behavior which are represented by instance variables and methods. Each instance of a class can have unique value for it’s instance variable.

For example,

Machine A may be powered up with Debian and have 8GB of RAM while Machine B can have installed Gentoo with 4GB of RAM. Also it is obvious that managing Machine that have installed Gentoo requires more knowledge – A behavior acting on its state. Here method is using instance variable values.


The JVM when parse a class, it make object of that kind. When you are writing a class, in actual you acting like a compiler telling your class what the object should know and how it should act. Every object of a particular type can have different value for same instance variable.

Every Instance of a class has the same method but it possible that all of them behave differently.

The OS class has 3 Instance variables namely OS Name, OS Type, OS Category.

OS

OS_Name

OS_Type

OS_Category

Boot()

Reboot()

scan()

The Boot() method boots one OS which is represented by OS Name for that instance. So if you boot() on one instance you will boot into Debian while on another instance you will boot into Gentoo. The method code, remains the same in either case.

Void Boot() { bootloader.bootos(OS_Name); }

You are already aware that the program starts to execute just after the main() method. You can pass values into you method.

For example you would like to tell you OS what services to start at boot as:

You are already aware that the program starts to execute just after the main() method. You can pass values into you method. For example you would like to tell you OS what services to start at boot as:
OS.services(apache2);

What you pass into methods are called arguments. You can use a variable with a type and a name inside a method. It is important to pass values with parameter if a method takes a parameter.

OS deb = debian();
deb.reboot(600);

Here the reboot method on the OS passes the value of 600 (reboot machine after 600 sec) as an argument to the method. Till now we have seen method always returning void, which means it don’t return you anything, simply as:

void main() { … … }

However you can ask your compiler to get exactly what you are desiring and your compiler won’t return you wrong types. You may simply do like:

int Integer() { … … return 70; }

You can send more than one value value to a method. You can do this by calling two parameter methods and sending it to arguments. Note variable type and parameter type must always match.

void numbers(int a, int b) { int c = a + b; System.out.print(“sum is” +c); }
Declare and Initialize Instance Variables

1. When you don’t know the value to initialize.

int a;
float b;
string c;

2. When the know the value to Initialize.

int a = 12;
float b = 11.23;
string c = tecmint;

Note: A instance variables are often confused with local variables, however there is a very thin line between them to differentiate.

3. Instance Variables are declared inside a class unlike local variables that are declared within a method.

4. Unlike Instance Variables, local variables must initialize before it can be used. The compiler will report error if you use local variable before it is initialized.

Encapsulation

You might have heard about encapsulation. It is a feature of most of the object oriented programming language which makes it possible to bind data and functions into a single component. Encapsulation is supported by class and protects codes from accidental damage by creating a wall around objects and hides their properties and methods, selectively.

We will expand encapsulation in details in the right tutorial when it is required. As of now it is sufficient for you to know What encapsulation is? What it does? And how it does?

That’s all for now. Keep connected for the next part of this Java Series “class and objects in Java and Make your First object in Java” while I am working on it. If you like the series and post let us know in the feedback.

Facebook Comments
Spread the love

Posted by News Monkey