Object Oriented ProgAnujming Concepts

Fundamentals of Classes
Objects: - An object is a self-contained component that contains properties and methods needed to make a certain type of data useful. An object’s properties are what it knows and its methods are what it can do. The project management application mentioned above had a status object, a cost object, and a client object, among others. One property of the status object would be the current status of the project. The status object would have a method that could update that status. The client object’s properties would include all of the important details about the client and its methods would be able to change them. The cost object would have methods necessary to calculate the project’s cost based on hours worked, hourly rate, materials cost, and fees.
In addition to providing the functionality of the application, methods ensure that an object’s data is used appropriately by running checks for the specific type of data being used. They also allow for the actual implementation of tasks to be hidden and for particular operations to be standardized across different types of objects. You will learn more about these important capabilities in Object-oriented concepts: Encapsulation.
Objects are the fundamental building blocks of applications from an object-oriented perspective. You will use many objects of many different types in any application you develop. Each different type of object comes from a specific class of that type.

If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior.
If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging, running
If you compare the software object with a real world object, they have very similar characteristics.
Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
Classes : -
A class is a blue print from which individual objects are created.
A class is used in object-oriented programming to describe one or more objects. It serves as a template for creating, or instantiating, specific objects within a program. While each object is created from a single class, one class can be used to instantiate multiple objects.
Example: -
class Rectangle {
double length;
double breadth;
}
// This class declares an object of type Rectangle.
class RectangleDemo {
public static void main(String args[]) {
Rectangle myrect = new Rectangle();
double area;
// assign values to myrect's instance variables
myrect.length = 10;
myrect.breadth = 20;
// Compute Area of Rectangle
area = myrect.length * myrect.breadth ;
System.out.println("Area is " + area);
}
}
Output : -
C:java>java RectangleDemo
Area is 200.0
Classes are a fundamental part of object-oriented programming. They allow variables and methods to be isolated to specific objects instead of being accessible by all parts of the program. This encapsulation of data protects each class from changes in other parts of the program. By using classes, developers can create structured programs with source code that can be easily modified.
A class can contain any of the following variable types.
Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various kinds of methods.
Creating Class Instances
In object-oriented progAnujming, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy, or instance. An instance variable is similar to and contrasts with a class variable. In languages like Java, C#, and C++ a class variable is defined using the Static variable declaration modifier while an instance variable is defined without said modifier.
A simple definition is that instance variables are things an object knows about itself, but the class does not know about. All instances of an object have their own copies of instance variables, even if the value is the same from one object to another. One object instance can change values of its instance variables without affecting all other instances. Changing the value of a class variable changes that value for all objects. Instance variables can be used by all methods of a class unless the method is declared as static. You access instance variables directly from their containing object instances.

Adding methods to a class
A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console. Now you will learn how to create your own methods with or without return values, invoke a method with or without paAnujeters, overload methods using the same names, and apply method abstraction in the progAnuj design. Creating Method: -
Considering the following example to explain the syntax of a method: -
public static int funcName(int a, int b) {
// body
}
* public static : modifier
* int: return type
* funcName: function name
* a, b: formal paAnujeters
* int a, int b: list of paAnujeters
Methods are also known as Procedures or Functions:
* Procedures: They don't return any value
* Functions: They return value
Method definition consists of a method header and a method body : -
modifier returnType nameOfMethod (PaAnujeter List) {
// method body
}
The syntax shown above includes:
* modifier: It defines the access type of the method and it is optional to use.
* returnType: Method may return a value.
* nameOfMethod: This is the method name. The method signature consists of the method name and the paAnujeter list.
* PaAnujeter List: The list of paAnujeters, it is the type, order, and number of paAnujeters of a method. These are optional, method may contain zero paAnujeters.
* method body: The method body defines what the method does with statements.
Example:
Here is the source code of the above defined method called max(). This method takes two paAnujeters num1 and num2 and returns the maximum between the two:
/** Returns the minimum between two numbers */
public class ExampleMinNumber{
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
/** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
This would produce the following result:
Minimum value = 6
Methods that Return Values:: -
"return" means in java when programming methods. Sometimes we need a method to return a value for us, for example if we create a character for a game and we forget the characters name, we could create a method that returns the name of that character for us. So far we have only learned about methods that are void, or they don't return anything,
Example: -
class Rectangle {
int length;
int breadth;
void setLength(int len)
{
length = len;
}
int getLength()
{
return length;
}
} class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
r1.setLength(20);
int len = r1.getLength();
System.out.println("Length of Rectangle : " + len);
}
}
C:java>java RectangleDemo
Length of Rectangle : 20
These are important things to understand about returning values : -
1 The type of data returned by a method must be compatible with the return type specified by the method. For example, if the return type of some method is boolean, you could not return an integer.
2 The variable receiving the value returned by a method (such as len, in this case) must also be compatible with the return type specified for the method.
3 Parameters should be passed in sequence and they must be accepted by method in the same sequence.
The void Keyword:: -
The void keyword allows us to create methods which do not return a value. Here, in the following example we're considering a void method methodRankPoints. This method is a void method which does not return any value. Call to a void method must be a statement i.e. methodRankPoints(255.7);. It is a Java statement which ends with a semicolon as shown below.
Example: -
public class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(255.7);
}
public static void methodRankPoints(double points) {
if (points >= 202.5) {
System.out.println("Rank:A1");
}
else if (points >= 122.4) {
System.out.println("Rank:A2");
}
else {
System.out.println("Rank:A3");
}
}
}
This would produce the following result:
Rank:A1
Using ‘this’ keyword
'this' is one of the keyword in java progAnujming language. 'this' can be used inside any method to refer to the current object. example:-
class Box
{
double width;
double height;
double depth;
Box(double w,double h,double d)
{
this.width=w;
this.height=h;
this.depth=d;
}
}
The significance of the keyword this would be realized in cases where we use inheritance. There may be cases where you have methods of the same name in both the current class and its parent. That is when the keyword "this" can be used to tell the JVM to invoke the method of the current class and not the parent class. this refers to the object with which the method inside a class is called. For instance, I call studentObject.getGrade(), in the getGrade() method to call my own method or variables, i.e. a private instance variable named grade, I would state: this.grade. Though inside the class any method or variable that is called without an object automattically calls the most local method or variable, i.e. I could just call grade for the above example given that I do not have a variable of the same name declared in the method. "this" is useful in those circumstances where there are more than one variable with the same name, just for example that I do have a variable named grade in my getGrade() method, to get my instance variable for my object I would simply call this.grade, thus to be safe use this for instance variables.
The keyword 'this' refers to the current instance. You can access any member of the class using this.. For example, if you have an Employee class with members 'firstName' and 'lastName' and let's say you have a private method that formats a full name everytime there is a change in either the first name or the last name. Then the formatFullName method can access the mebers using 'this' keyword as follows:private void formatFullName() { this.fullName = String.format("%s %s", this.firstName, this.lastName);}Note that however the use of keyword 'this' is not required in this example. An example where use of this keyword is necessary is when calling another contsrictor of the same class.
This keyword holds the reference of the current object . for example if we do like:- Anuj r= new Anuj(2); //Anuj is a class here here 2 is passed in the constructor and it will be hold like:- Anuj(int a) { this.b=a; //int b is a variable here } then here "this" will hold the reference of r and internal working of this is like:- Anuj r=new Anuj(r,2); and holds like Anuj(this d, int a) { d.b=a; }


Free Web Hosting