Upgrade to Pro — share decks privately, control downloads, hide ads and more …

IEU SEC Java

IEU SEC Java

Avatar for Yasin Sinan Kayacan

Yasin Sinan Kayacan

October 19, 2015
Tweet

Other Decks in Education

Transcript

  1. Object Oriented Design There are 4 fundamental ideas of OOP.

    - Abstraction - Polymorphism - Inheritance - Encapsulation
  2. Abstraction Abstraction means we have to focus on the essential

    qualities of something rather than one specific example. It means we automatically will discard what’s unimportant or irrelevant.
  3. Encapsulation Think something like a space capsule or a medication

    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.
  4. Inheritance The idea of inheritance is first a great form

    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.
  5. Inheritance Then later on you figure out your application will

    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.
  6. Polymorphism Two objects respond to the same message with different

    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.”
  7. Polymorphism It lets us automatically do the correct behavior even

    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.
  8. Polymorphism We can then create more specialized subclasses that can

    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.
  9. Data Types 1. Primary Data Type: The eight data types

    of Java are byte, short, int, long, float, double, char and boolean. 2. Non-Primitive Data Types: string, array etc.
  10. Variables • Variable name can consist of Capital letters A-Z,

    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.
  11. /* variable definition and initialization */ int width, height=5; char

    letter='C'; float age, area; double d; /* actual initialization */ width = 10; age = 26.5;
  12. Types of Variable In Java • Local variables • Instance

    variables • Class/Static variables
  13. Local variables: A variable that is declared within the method

    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.
  14. class A { int amount = 100; //instance variable static

    int pin = 2315; //static variable void method() { int age=35; //local variable } }
  15. Java Modifiers Access control modifier There are four access modifiers

    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)
  16. Non-Access modifier There are five non-access modifiers available in Java,

    used to achieve many other functionality. • Modifier to finalizing the implementations of classes, methods, and variables (final)
  17. class Phone { final int PRICE_MIN = 999; final int

    PRICE_MAX = 5600; //final variable final void display() { //final method System.out.println("Min Price is" + PRICE_MIN); System.out.println("Max Price is" + PRICE_MAX ); } }
  18. class Programming { public static void main(String[] args) { display();

    } static void display() { System.out.println("I love to programming in Java. Hello IEU SEC :)"); } }