عرض تقديمي في PowerPoint.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Imam Mohammad Ibn Saud Islamic University Applied College Computer Sciences Program Chapter 1 Basic principles of object-oriented programming (OOP) Programming 3 ...

Imam Mohammad Ibn Saud Islamic University Applied College Computer Sciences Program Chapter 1 Basic principles of object-oriented programming (OOP) Programming 3 OUTLINE  Motivations  Encapsulation  Inheritance: - The concept of inheritance. - Addressing inherited methods. - Replacement (overriding) inherited methods.  Polymorphism: - Types of polymorphism. - The basics of polymorphism.  Abstraction  References 2 Motivations 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. 3 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 cod OOP makes it possible to create full reusable applications with less code and shorter development time ❑ easier to maintain, modify and debug. 4 Encapsulation in Java Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines. We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. 5 Advantage of Encapsulation in Java: ❖ It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. ❖ You can write the logic not to store the negative numbers in the setter methods. ❖ It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members. ❖ The encapsulate class is easy to test. So, it is better for unit testing. ❖ The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in 6Java. Simple Example of Encapsulation in Java Let's see the simple example of encapsulation that has only one field with its setter and getter methods. 7 8 Output: Java Inheritance Java Inheritance (Subclass and Superclass) Inheritance is a mechanism in which a class inherits properties and behaviors from another class called the superclass or subclass. In Java, it is possible to inherit attributes and methods from one class to another. 9 Types of inheritance in java: On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later. 10 Figure1: Types of inheritance in java 11 Single Inheritance When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance. 12 Single Inheritance Example: class Animal{ void eat(){ System.out.println("eating..."); } } class Dog extends Animal{ void bark(){ System.out.println("barking..."); } } class TestInheritance{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); } } Output: 13 Multilevel Inheritance When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance. 14 Multilevel Inheritance Example Output: 15 Hierarchical Inheritance When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance. 16 Hierarchical Inheritance Example 17 Output: Terms used in Inheritance Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. 18 The syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields } The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. 19 Java Inheritance Cont. We group the "inheritance concept" into two categories: subclassssalc rehtona morf stirehni taht ssalc eht - )dlihc( superclassmorf detirehni gnieb ssalc eht - )tnerap( To inherit from a class, use the extends keyword. In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass): 20 class Vehicle { Example1 -3: protected String brand = "Ford"; // Vehicle attribute Example: public void honk )( { // Vehicle method System.out.println)"Tuut, tuut!”(; } } class Car extends Vehicle { private String modelName = "Mustang"; // Car attribute public static void main)String[] args) { //Create a myCar object Car myCar = new Car )(; //Call the honk() method (from the Vehicle class) on the myCar object myCar.honk ;)( //Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class System.out.println)myCar.brand + " " + myCar.modelName); } 21 } Output: 22 Example explained Did you notice the protected modifier in Vehicle? We set the brand ni etubirtta Vehiclereifidom ssecca detcetorp a ot If it was set to private, the Car class would not be able to access it. Why And When To Use "Inheritance?" -It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class. 23 The final Keyword If you don't want other classes to inherit from a class, use the final keyword: If you try to access a final class, Java will generate an error : In the Example below, we will used the final keyword: 24 Example1 -4: final class Vehicle { protected String brand = "Ford"; public void honk() { System.out.println("Tuut, tuut!"); } } class Main extends Vehicle { private String modelName = "Mustang"; public static void main(String[] args) { Main myFastCar = new Main(); myFastCar.honk(); System.out.println(myFastCar.brand + " " + myFastCar.modelName); } } The output will be something like this: 25 Java Polymorphism Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous slides, Inheritance.ssalc rehtona morf sdohtem dna setubirtta tirehni su stel Polymorphism.sksat tnereffid mrofrep ot sdohtem esoht sesu This allows us to perform a single action in different ways. 26 Types of Java Polymorphism In Java Polymorphism is mainly divided into two types: - Compile-time Polymorphism (overloading) - Runtime Polymorphism (overriding) 27 Compile-time Polymorphism Overloading When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.. 28 change in the type of arguments. changes in the number of arguments 29.. Runtime Polymorphism (overriding)  In Java, runtime polymorphism occurs when two or more classes are related through inheritance. We must create an “IS-A” relationship between classes and override a method to achieve runtime polymorphism.  For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.): 30 Example1 -5: we use the extends keyword to inherit from a class. 31 Now we can create Pig and Dog objects and call the animalSound() method on both of them : class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } Example1 -6: } class Pig extends Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } } class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bow wow"); } } class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myPig = new Pig(); Animal myDog = new Dog(); myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); 32 } } Output: 33 Java Polymorphism Cont.  Why And When To Use "Inheritance" and "Polymorphism"?  It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class. 34 Abstraction in Java  Abstraction is a process of hiding the implementation details and showing only functionality to the user.  Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery. 35 Abstract class in Java A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated. Example of abstract class abstract class A{ } 36 Abstract Method in Java A method which is declared as abstract and does not have implementation is known as an abstract method. Example of abstract method abstract void printStatus();//no method body and abstract 37 Example of Abstract class that has an abstract method In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class. abstract class Bike{ abstract void run(); } class Honda4 extends Bike{ void run(){System.out.println("running safely");} public static void main(String args[]){ Bike obj = new Honda4(); obj.run(); 38 } } Abstract Classes and Methods Data abstraction dna sliated niatrec gnidih fo ssecorp eht si.resu eht ot noitamrofni laitnesse ylno gniwohs Abstraction can be achieved with either abstract classes ro interfaces txen eht ni tuoba erom nrael lliw uoy hcihw(.)retpahc The abstract keyword is a non-access modifier, used for classes and methods: 39 Example1 -7: From the example above, it is not possible to create an object of the Animal class: Animal myObj = new Animal)(; // will generate an error 40 To access the abstract class, it must be inherited from another class. Let's convert the Animal class we used in the Polymorphism slide to an abstract class: Example1 -8: 41 Why And When To Use Abstract Classes and Methods? To achieve security - hide certain details and only show the important details of an object. Note: Abstraction can also be achieved with Interfaces, which you will learn more about in the next chapters. 42 References: 1. https://www.w3schools.com/ 2. https://www.javatpoint.com/abstract-class-in-java 43

Use Quizgecko on...
Browser
Browser