capsule or even a food container. It’s the idea of surrounding something. Not just to keep the contents together but also to protect them. In object orientation, this refers to the idea of taking our attributes and our behaviors together and bundling them together in the same unit, in the same class. But, it’s really more than that. We also want to restrict access to the inner working of that class or any object based on that class and this is referred to as information hiding. The principal is that the object should not reveal anything about itself except what is absolute necessary for others parts of application to work.
of code reuse. We can create a new class but instead of writing it from scratch, we can base it on an existing class. So, lets you start off by defining in your application a Person class as shown below.
need another class and this one called Customer but you realize that this customer class is exactly the same as the person class and the only difference is it also has a customer number. Now, you don’t want to add that customer number to your person class because we’re trying to use abstraction, we’re trying to only focus on essentials and not all of you person objects will be your customer. You could do this by creating a completely different class but in object orientation a better way is that we will create a new class called Customer that inherits from Person class. The phrase we use is Customer inherits from Person. That means our new customer class automatically has everything that the Person class has, all its attributes, all its behaviors without us having to write any code. When we just say in our customer class, what we want to add to it. In this case, we add a customer number or another new attribute or another add a new method. Now the term that is most commonly used for this relationship is that the Person class is the superclass and the Customer class is the subclass. We can also hear this described as the parent class and the child class. One of the best thing about inheritance is not just the time you save and being able to reuse code but what it allows us to use the last of our 4 key terms, polymorphism.
behaviors; the sender doesn't have to care. “Here is a very beautiful definition of polymorphism. A boy starts LOVE with the word FRIENDSHIP. but A girl ends LOVE with the same word FRIENDSHIP. Word is the same, but functionality is different. That’s polymorphism.”
if what we’re working with could take one of many different forms. Sounds little vague? So, here’s an example of polymorphism that you’ve probably used without even thinking about it. The + sign. Well, what does it do? In a lot of languages it depends. If we are adding two integer variables with the + sign, it will numerically add them. But on the other hand, if the two variables are strings, it will concatenate them. It’ll automatically do the correct behavior but a different behavior when what we’ve given it could have one of many different forms. Now, this example is built into a lot of languages but we can use the same idea with our own classes, with our own objects. So, here’s an example.
inherit from it like a SavingsAccount class, a CheckingAccount class, an InvestmentAccount class so they all inheriting, they all share the basic definition. So, they’ll have an accountName, a balance, they can withdraw, they can deposit but the SavingsAccount might add an interestRate, an attribute that a CheckingAccount does not have but then it gets little more complex. Say the business rule says that if you withdraw from an InvestmentAccount, well you should get a penalty if it not 30 days notice. So, it’s a bit more complex behavior. Now, that withdraw behavior was originally defined in the BankAccount class that I’m already inheriting but I can provide a more specialized version of that just for the InvestmentAccount class and this is referred to as overriding the method of the superclass.That just means I write it again. So, I’m inheriting what’s useful but I can override behaviors when that’s useful. Polymorphism lets us work freely with objects that have been created from any of these classes so I can have an array of accounts that have 10000 of these different objects loaded into it and I know that I can call the withdraw() method on any of them without knowing exactly what class it was instantiated from and it would do the correct behavior for each one just as using the + sign would automatically switch between addition and concatenation. So it’s flexibility. Polymorphism lets us do the right thing at the right time.
lowercase letters a-z, digits 0-9, and two special characters such as underscore and dollar Sign. • The first character must be a letter. • Blank spaces cannot be used in variable names. • Java keywords can not be used as variable names. • Variable names are case sensitive.
that is called local variables. Instance variables: A non-static variable that is declared within the class but not in the method is called instance variable. Class/Static variables: A variable that is declared with static keyword in a class but not in the method is called static or class variable.
available in Java, used to set access levels for classes, variable methods and constructor. • Scope only inside the same package (default) • Scope is visible to world (public) • Scope within the package and all subclasses (protected) • Scope only within the classes only (private)