140 likes | 263 Views
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;
E N D
04. Review OOP with Java Prof. OumSaokosal 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; 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;
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"); } }
Inheritance in UML Diagram Parent Child1 Child2 Child3
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"; } }
Abstract class in UML Diagram <<abstract>> Bird Duck Swallow
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
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
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;
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.
Polymorphism What is polymorphism? Polymorphism is a mechanism to allow a single variable to refer to objects from different classes.
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"); } }