1 / 14

04. Review OOP with Java

04. Review OOP with Java. Prof. Oum Saokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 oum_saokosal@yahoo.com. Agenda. Array Class/Object Inheritance Abstract Class Overridden Methods Interface Polymorphism. Array. int [] a = new int [10] ; a[0]=10;

davis-king
Download Presentation

04. Review OOP with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 04. Review OOP with Java Prof. OumSaokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 oum_saokosal@yahoo.com

  2. Agenda Array Class/Object Inheritance Abstract Class Overridden Methods Interface Polymorphism

  3. Array • int[] a = new int[10]; a[0]=10; a[1]=20; a[9]=45; • double[] d = new double[5][10]; d[0][0]=9.9; d[0][1]=3.14; a[4][9]=9.8;

  4. Class/Object public class Aclass { public Aclass(){//it’s called constructor Bclass b = new Bclass(); //b is object/instance b.hello(); //b invokes hello() method } public static void main(String[] args){ new Aclass(); } } class Bclass { public void hello(){ //hello() is a method System.out.print("Hello Everyone"); } }

  5. Inheritance in UML Diagram Parent Child1 Child2 Child3

  6. Inheritance public class Child extends Parent{ public Child(){ System.out.println(money); System.out.println(gold); System.out.println(rich()); System.out.println(job()); } public static void main(String[] args){ new Child(); } } class Parent{ public double money = 500000; public double gold = 200; public boolean rich(){ return true; } public String job(){ return "Doctor"; } }

  7. Abstract class in UML Diagram <<abstract>> Bird Duck Swallow

  8. Abstract Class public abstract class Bird { public String color; public abstractbooleanisFlyable(); public void sound(){ System.out.println("Default”); } } class Duck extends Bird{ public booleanisFlyable() { return false; } public void sound(){ System.out.println("Quack "); } } class Swallow extends Bird{ public booleanisFlyable() { return true; } } Overridden Method

  9. Interface public interface Flyable { public String fly(); } class Chicken implements Flyable{ @Override public String fly() { return "Low and near"; } } class Swallow implements Flyable{ @Override public String fly() { return "High and far"; } } <<interface>> Flyable Chicken Swallow

  10. Abstract class vs Interface (Same) Abstract class Interface All classes implemented the interface have to override all methods. You cannot use new operator to create an instance from an interface. Flyable fly = new Flyable(); Interface can be used as a type. Flyable fly; • All subclasses of the abstract class have to override all abstract methods. • You cannot use new operator to create an instance from an abstract class. Bird b = new Bird(); • Abstract class can be used as a type. Bird b;

  11. Abstract class vs Interface (Different) Abstract class Interface To declare an interface, use abstract keyword. public interface B{ } A class can implement more than one interface. class A implements C, D, E{ } In relationship, we A has C, D, and E. • To declare an abstract class, use abstract keyword. public abstract class B{ } • A class can extend only one abstract class. class A extends B{ } • In relationship, we say A is B.

  12. Polymorphism What is polymorphism? Polymorphism is a mechanism to allow a single variable to refer to objects from different classes.

  13. Polymorphism public class MyPoly { public static void main(String[] args) { Bird b; // b is Bird b = new Duck(); // b refers to Duck b.sound(); // b invokes Duck’s sound b = new Chick();// b refers to Chick b.sound(); //b invokes Chick’s sound } } abstract class Bird{ public abstract void sound(); } class Duck extends Bird{ public void sound(){ System.out.println("Quack"); } } class Chick extends Bird{ public void sound(){ System.out.println("Cheep"); } }

  14. Go on the next slide

More Related