Presentation is loading. Please wait.

Presentation is loading. Please wait.

AD 300 ---- Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.

Similar presentations


Presentation on theme: "AD 300 ---- Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism."— Presentation transcript:

1 AD 300 ---- Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism

2 Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding its own. A class can implement an interface, implementing all the specified methods. Inheritance implements the “is a” relationship between objects. 2

3 Inheritance (cont’d) 3 subclass or derived class superclass or base class extends

4 Inheritance (cont’d) In Java, a subclass can extend only one superclass. In Java, a subinterface can extend one superinterface 4

5 Inheritance (cont’d) Inheritance plays a dual role: A subclass reuses the code from the superclass. A subclass (or a class that implements an interface) inherits the data type of the superclass (or the interface) as its own secondary type. 5

6 Inheritance (cont’d) Inheritance leads to a hierarchy of classes and/or interfaces in an application: 6 Person WomenMan

7 Inheritance (cont’d) An object of a class at the bottom of a hierarchy inherits all the methods of all the classes above. It also inherits the data types of all the classes and interfaces above. Inheritance is also used to extend hierarchies of library classes, reusing the library code and inheriting library data types. 7

8 Encapsulation Encapsulation means that all data members (fields) of a class are declared private. Some methods may be private, too. The class interacts with other classes (called the clients of this class) only through the class’s constructors and public methods. Constructors and public methods of a class serve as the interface to class’s clients. 8

9 Encapsulation (cont’d) Ensures that structural changes remain local: Usually, the structure of a class (as defined by its fields) changes more often than the class’s constructors and methods. Encapsulation ensures that when fields change, no changes are needed in other classes (a principle known as “locality”). 9

10 10 Polymorphism Polymorphism means many (poly) shapes (morph) In Java, polymorphism refers to the fact that you can have multiple methods with the same name in the same class There are two kinds of polymorphism: Overloading Two or more methods with different signatures Overriding Replacing an inherited method with another having the same signature

11 11 Overload In Java, two methods have to differ in their names or in the number or types of their parameters foo(int i) and foo(int i, int j) are different foo(int i) and foo(int k) are the same foo(int i, double d) and foo(double d, int i) are different

12 12 Overloading class Test { public static void main(String args[]) { myPrint(5); myPrint(5.0); } static void myPrint(int i) { System.out.println("int i = " + i); } static void myPrint(double d) { // same name, different parameters System.out.println("double d = " + d); } } int i = 5 double d = 5.0

13 13 Why overload a method? So you can use the same names for methods that do essentially the same thing Example: println(int), println(double), println(boolean), println(String), etc. So you can supply defaults for the parameters: int increment(int amount) { count = count + amount; return count; } int increment() { return increment(1); } Notice that one method can call another of the same name So you can supply additional information: void printResults() { System.out.println("total = " + total + ", average = " + average); } void printResult(String message) { System.out.println(message + ": "); printResults(); }

14 14 Overriding This is called overriding a method Method print in Dog overrides method print in Animal A subclass variable can shadow a superclass variable, but a subclass method can override a superclass method class Animal { public static void main(String args[]) { Animal animal = new Animal(); Dog dog = new Dog(); animal.print(); dog.print(); } void print() { System.out.println("Superclass Animal"); } } public class Dog extends Animal { void print() { System.out.println("Subclass Dog"); } } Superclass Animal Subclass Dog

15 15 Summary You should overload a method when you want to do essentially the same thing, but with different parameters You should override an inherited method if you want to do something slightly different than in the superclass


Download ppt "AD 300 ---- Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism."

Similar presentations


Ads by Google