Okay in this chapter we are going to go into the object oriented programming of Java. So, in this chapter we will cover object oriented programming. Why is grass Why is an object Why is property why they met does polymorphism why they matters and capsulation and why they matters inheritance okay object oriented programming as we differentiate between procedural and object oriented programming in chapter one, we mentioned that job is actually an object oriented programming language. So, how does object oriented programming involve object oriented programming represent a concept of objects and associated procedure or function known as method. Objects are the instance of process useful interaction with one another in the design of an application or program. As such object oriented programming provides that the organization and model import a real object in program design.
So, how do we create a class and object in the Java programming? So for our console calculator, we will create my mess class. So I will come to host package here in a new Java class my mix okay and finish that see why he's inside my mess cross. Okay, so in this class, I'm going to We organize organize all my co I'm going to put in order Massa calculation as a meta into this mess cross. Okay, so first I Korea ah first number second number okay na ba bus number ba ba second number and I'm going to create a property met a these are the property must get first number set first number get second number set second number Qaeda shoot ways to create the property matters here. He Okay Never mind I will create them.
So probably get first number okay. I think at first number should be taba. Okay, so I'm going to return these doc first number. So for Bri, Baba, guests first number, second number read these and number get out there. Get down we have guns sec okay Bri boy set for us number for us number k d days no first equal first number Bri boy say second number second number candies the second number it was again number okay then we should be Create another okay constructor method. I mess in my mess to toe contractor method.
Okay, Bobby, my mass plus constructor method. Then be my first number. Second number. Do you stop first number it will number the second set second Then we look to the okay the next one we will create order order meta for the mass calculation. So double sum and then we have Dabba samples number second number okay so fabri GABA some okay. So da bovie return these.
No first number, these second second number Bri na ba ba ba ba ba ba said Ken We can number okay Bobby, Bobby, Bobby substrate. Nice talk. First number, second. Number three subtract number, the second number we bus number minus set number Okay, this one should be minus top track pro die and deviation copy base some okay dhaba Proto. So this one should be times pro that this one should be times okay subtract deviation is one should be by deviation the song should be okay so I have created my own Class Okay, so now how are we going to add this math class into the console calculator? So we create a mess object and then we add in the we change our calculation by using the object some object method.
Okay? So I in this one okay? My mask and he knew Mine okay. The DVD number one, TV number two okay. Some I will change to em some is our choose to m dot subtract is our change to m.is on I will change to em the division. Okay.
I should be getting around the same result so I will run the Quincy Okay addition one okay first number one second number two okay results will be three yes this is correct. So this is I will say Rafi This is how we create a crossing the Java programming. So I will say a Y is a class. So a class is a footprint of an object, it consists of the following members. So, in class we have the class variables. Then we have the property matters you can see the other getters Are the sectors we've published modifier then we have the constructor then we have vectors.
So what is an object? An object is the instance of a class it can be the crest follows. So my mess is cross then VCA mth is the object or the other is the object and then equal new my mess. Cross. So this one, my mess is actually the constructor. Okay, this is the constructor.
So in a course Ah, in the console calculator course. I write my mess M equal new my mess dhaba first number, double second. Number so if we look into the my math class I am actually using this constructor method okay my mess the first number the second number so I'm using the same construct constructor Mehta my mess dhaba number one, number two. I'm using this constructor method. I can also use this constructor method. Just as you see I use the first one.
Then I have to set my own set of first number. Set, first number two dB, number one ladies. So second dbx I can also do this way by prefer to use the second quarter method without foster kill you he says Rafi wise ah ob Giada my mess object in the console calculator. Ky is an object and then to access the property and methods of the my mess object we can set to something empty Don't get first number and tears don't set the first number. So, get the first number is to retrieve the value of the first number below but in empty object and then set first number is to set a value of the first number in the object we can do mth talks some is to call the summit in my math class okay. So, in our my next class I will say get first time ba and then return this dot first time return the stock first number actually refer to the ISA class variable first number, return this dot second number refer to the these are class variables second number so to set the first time Number Ah, so we have this top first number, we should refer to the class variable first number equal this parameter the second number, so the same, the stocks that can number your photo this one equal to the paramita.
So in my constructor method one is the one constructor method don't have any parameters. And then the second constructor method I have two parameters first number and second number. So when I use this second constructor method, I actually set the first number class variable to the pyramid And the second number class variable to this parameter then I call the my mess class object dot sum that means I call in this method these dot first number you see is a cross first number brass this dot second number which is the cross second number Okay, I can do the other ways probably double sum and then I have some of the parameters. Then I guess redonda ah some of the parameters value. Some other parameters are the variables Okay then subtitles. So the same product also the same and then division also the same.
So, what is the property of the cluster property of a class is a member that provides a mechanism to read write or compute the value of private few available for Java properties and getters and setters method. So, get a variable one set variable one, so, we can have a private web Robo one here we have the get Robo one method and then set the robo one method. Okay, next we are going to talk about polling Pisani now object oriented programming polymorphism means having many forms. So polymorphism can also be method overloading. So, method overloading means, overloading take place when a few methods with the same name, but different parameters appear in the same class he offers some form or flexibility to our method. So, if we look into the mess class, so, this semester is actually over load the meta data first summit ah we have no parameters.
By the second summit we have some parameters inside these two meta rd have the same name. So, they are called overloading method Okay, so, how do we use the overloading method so, we can do something Id double resolve Annika m dot sum double result to equal m dot sum one to the book both have the same result they are both the same. Okay then next we will talk about encapsulation. So, a data storage within a class can be encapsulate or hidden. any external class can only access to the data using the meta sada property meta variable storing the sensitive data values can be encapsulated with modifiers as follows. So far poppy modifier it means The variable or the cross or the they can all be used or accessed from any anyplace.
But that means I can use or accessing containing classes or types derive internet can use all assessing current as embryo ni, private can use or SS in the continuing care Bondy. Okay, so next we talked about inheritance. Okay inheritance our definition of our class in terms of another class allows a derived class to inherit some members of the base class. He is the task of creating and maintaining an application and it provides reuse of code functionality and promote faster implementation time. So, how do we create inheritance So far ah console application console application or console calculator we are going to create a summation cross then we are going to inherit from the my next class okay I think we can try to create a summation class so I in the same class here okay submission okay puppet cross site to inherit from my mess class, we've had to use a stand my mess class okay.
So, we can see that we are doing this public class summation as stands my next class. Let me have that constructor method here. Then we have the get result method which is a summation method okay so povery submission This is the concept that we have a summation i get results integer SMT try by remove the integer as integer Why are because I'm going to use the first number and second number in my next class. So I removed this one. So I say should be integer S equal first number, first number, integer and integer y equals integer can now Okay first number second number so by era I'm sure we'll be okay non static variable. So, I have to remove the static okay.
So, this first number and second number actually is from these my next class first number second number ah why we can use the first number and second number in my mess cross is because our submission Class A standard my next class. So, this means that our submission inherits my next class. So, this is the strength of the inheritance in Java programming So I can implement summation class by doing something like this I don't think I can do em Boston here. By should be a total mission equals new submission by so few of you something essential be my meds, check core okay. So, I can do something like this summation sum equal new summation some set the first number to be DB number one some dos set set a number to be DB number two. Now I can do the submission though ah get results Okay, so this one should be the kingdom okay so we that I can ah Kampala And I can run the program again.
Okay, so number five. So first number one 100 I still get Pfizer, Pfizer Oh, this is the correct result. Okay. So with that we have come to the end of chapter five. In chapter five he covers our causes and objects polymorphism, encapsulation and inheritance. In the next chapter we are driving to test my name using Java