Introduction To OOPs Flashcards
What is Object oriented programming?
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
Tip: The “Don’t Repeat Yourself” (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.
What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
class
Fruit
objects
Apple
Banana
Mango
Another example:
class
Car
objects
Volvo
Audi
Toyota
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and methods from the class.
What makes an object unique?
The Identity of the object
What is the State of an object
[Attributes ,Field]
Java Class Attributes
In the previous chapter, we used the term “variable” for x in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class.
Example:
Create a class called “Main” with two attributes: x and y:
public class Main {
int x = 5;
int y = 3;
}
Accessing Attributes
You can access attributes by creating an object of the class, and by using the dot syntax (.):
The following example will create an object of the Main class, with the name myObj. We use the x attribute on the object to print its value.
Example
Create an object called “myObj” and print the value of x:
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Modify Attributes
You can also modify attribute values:
Example
Set the value of x to 40:
public class Main {
int x;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 40;
System.out.println(myObj.x);
}
}
Override existing Value
Change the value of x to 25:
public class Main {
int x = 10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}
If you don’t want the ability to override existing values, declare the attribute as final:
Example
public class Main {
final int x = 10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 25; // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}
The final keyword is useful when you want a variable to always store the same value, like PI (3.14159…).
Multiple Objects
If you create multiple objects of one class, you can change the attribute values in one object, without affecting the attribute values in the other:
Example
Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj1 = new Main(); // Object 1
Main myObj2 = new Main(); // Object 2
myObj2.x = 25;
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Outputs 25
}
}
Multiple Attributes
You can specify as many attributes as you want:
Example
public class Main {
String fname = “John”;
String lname = “Doe”;
int age = 24;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(“Name: “ + myObj.fname + “ “ + myObj.lname);
System.out.println(“Age: “ + myObj.age);
}
}
What is the IDENTITY of an object?
What is the Behavior of an Object
[Method]
Java Class Methods
You learned from the Java Methods chapter that methods are declared within a class, and that they are used to perform certain actions.
ExampleGet your own Java Server
Create a method named myMethod() in Main:
public class Main {
static void myMethod() {
System.out.println(“Hello World!”);
}
}
myMethod() prints a text (the action), when it is called. To call a method, write the method’s name followed by two parentheses () and a semicolon;
myMethod() prints a text (the action), when it is called. To call a method, write the method’s name followed by two parentheses () and a semicolon;
Example
Inside main, call myMethod():
public class Main {
static void myMethod() {
System.out.println(“Hello World!”);
}
public static void main(String[] args) {
myMethod();
}
}
Access Methods With an Object
Example
Create a Car object named myCar. Call the fullThrottle() and speed() methods on the myCar object, and run the program:
// Create a Main class
public class Main {
// Create a fullThrottle() method
public void fullThrottle() {
System.out.println(“The car is going as fast as it can!”);
}
// Create a speed() method and add a parameter
public void speed(int maxSpeed) {
System.out.println(“Max speed is: “ + maxSpeed);
}
// Inside main, call the methods on the myCar object
public static void main(String[] args) {
Main myCar = new Main(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}
// The car is going as fast as it can!
// Max speed is: 20
// Outputs “Hello World!”
Example explained
1) We created a custom Main class with the class keyword.
2) We created the fullThrottle() and speed() methods in the Main class.
3) The fullThrottle() method and the speed() method will print out some text, when they are called.
4) The speed() method accepts an int parameter called maxSpeed - we will use this in 8).
5.In order to use the Main class and its methods, we need to create an object of the Main Class.
6) Then, go to the main() method, which you know by now is a built-in Java method that runs your program (any code inside main is executed).
7) By using the new keyword we created an object with the name myCar.
8) Then, we call the fullThrottle() and speed() methods on the myCar object, and run the program using the name of the object (myCar), followed by a dot (.), followed by the name of the method (fullThrottle(); and speed(200);). Notice that we add an int parameter of 200 inside the speed() method.
Remember that..
The dot (.) is used to access the object’s attributes and methods.
To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon (;).
A class must have a matching filename (Main and Main.java).