3. OOP basics, Class, object
3. OOP basics, Class, object
• Polymorphism
• Inheritance
• Encapsulation
• Abstraction
• Classes
• Objects
• Instance
• Method
• Message Passing
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.
Benefits of OOP
Something has gone wrong, and you have no idea where to look. Is the problem in
the Widget file, or is it the WhaleFlumper? Will you have to trudge through that
“sewage.c” file? Hope you commented your code!
That’s the beauty of encapsulation. Objects are self-contained, and each bit of
functionality does its own thing while leaving the other bits alone. Also, this
modality allows an IT team to work on multiple objects simultaneously while
minimizing the chance that one person might duplicate someone else’s
functionality.
Suppose that in addition to your Car object, one colleague needs a RaceCar object,
and another needs a Limousine object. Everyone builds their objects separately but
discover commonalities between them. In fact, each object is really just a different
kind of Car. This is where the inheritance technique saves time: Create one generic
class (Car), and then define the subclasses (RaceCar and Limousine) that are to
inherit the generic class’s traits.
Of course, Limousine and RaceCar still have their unique attributes and functions.
If the RaceCar object needs a method to “fireAfterBurners” and the Limousine
object requires a Chauffeur, each class could implement separate functions just for
itself. However, because both classes inherit key aspects from the Car class, for
example the “drive” or “fillUpGas” methods, your inheriting classes can simply
reuse existing code instead of writing these functions all over again.
What if you want to make a change to all Car objects, regardless of type? This is
another advantage of the OO approach. Simply make a change to your Car class,
and all car objects will simply inherit the new code.
iii. Flexibility through polymorphism
Riffing on this example, you now need just a few drivers, or functions, like
“driveCar,” driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some
traits with LimousineDrivers, but other things, like RaceHelmets and
BeverageSponsorships, are unique.
This isn’t to say that OOP is the One True Way. However, the advantages of
object-oriented programming are many. When you need to solve complex
programming challenges and want to add code tools to your skill set, OOP is your
friend — and has much greater longevity and utility than Pac-Man or parachute
pants.
Java - What are Classes and Objects?
• Object − Objects have states and behaviors. Example: A dog has states - color,
name, breed as well as behaviors – wagging the tail, barking, eating. An object is
an instance of a class.
• Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type support.
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
When the individual objects are created, they inherit all the variables and methods
Object Definitions:
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Syntax to declare a class:
1. class <class_name>{
2. field;
3. method;
4. }
What is Object in Java?
OBJECT is an instance of a class. An object is nothing but a self-contained
component which consists of methods and properties to make a particular
type of data useful. For example color name, table, bag, barking. When you
send a message to an object, you are asking the object to invoke or execute
one of its methods as defined in the class.
From a programming point of view, an object can include a data structure, a
variable, or a function. It has a memory location allocated. The object is
designed as class hierarchies.
Syntax
ClassName ReferenceVariable = new ClassName();
What is the Difference Between Object & Class?
A class is a blueprint or prototype that defines the variables and the
methods (functions) common to all objects of a certain kind.
An object is a specimen of a class. Software objects are often used to model
real-world objects you find in everyday life.
Instance variable in Java
A variable which is created inside the class but outside the method is known as an
instance variable. Instance variable doesn't get memory at compile time. It gets
memory at runtime when an object or instance is created. That is why it is known
as an instance variable.
Method in Java
Advantage of Method
o Code Reusability
o Code Optimization
The new keyword is used to allocate memory at runtime. All objects get memory
in Heap memory area.
In this example, we have created a Car class which has two data members id and
name. We are creating the object of the Car class by new keyword and printing the
object's value.
C2.id=9500;
C2.name="Tesla";
C2.model="Cavier";
C2.price=50000;
}
}
In real time development, we create classes and use it from another class. It is a
better approach than previous one. Let's see a simple example, where we are
having main() method in another class.
We can have multiple classes in different Java files or single Java file. If you
define multiple classes in a single Java source file, it is a good idea to save the file
name with the class name which has main() method.
File: TestStudent1.java
1. By reference variable
2. By method
3. By constructor
Initializing an object means storing data into the object. Let's see a simple example
where we are going to initialize the object through a reference variable.
File: TestStudent2.java
1. class Student{
2. int id;
3. String name;
4. }
5. class TestStudent2{
6. public static void main(String args[]){
7. Student s1=new Student();
8. s1.id=101;
9. s1.name="Sonoo";
10. System.out.println(s1.id+" "+s1.name);//printing members with a white space
11. }
12.}
We can also create multiple objects and store information in it through reference
variable.
File: TestStudent.java
1. class Student{
2. int id;
3. String name;
4. }
5. class TestStudent{
6. public static void main(String args[]){
7. //Creating objects
8. Student s1=new Student();
9. Student s2=new Student();
10. //Initializing objects
11. s1.id=101;
12. s1.name="Sonoo";
13. s2.id=102;
14. s2.name="Amit";
15. //Printing data
16. System.out.println(s1.id+" "+s1.name);
17. System.out.println(s2.id+" "+s2.name);
18. }
19.}
In this example, we are creating the two objects of Student class and initializing the
value to these objects by invoking the insertRecord method. Here, we are
displaying the state (data) of the objects by invoking the displayInformation()
method.
File: TestStudent4.java
1. class Student{
2. int rollno;
3. String name;
4. void insertRecord(int r, String n){
5. rollno=r;
6. name=n;
7. }
8. void displayInformation(){System.out.println(rollno+" "+name);}
9. }
10.class TestStudent4{
11. public static void main(String args[]){
12. Student s1=new Student();
13. Student s2=new Student();
14. s1.insertRecord(111,"SAYMA");
15. s2.insertRecord(222,"AZAN");
16. s1.displayInformation();
17. s2.displayInformation();
18. }
19.}