{"id":15,"date":"2010-08-20T04:46:39","date_gmt":"2010-08-20T04:46:39","guid":{"rendered":"http:\/\/nvntung.wordpress.com\/?p=15"},"modified":"2010-08-20T04:46:39","modified_gmt":"2010-08-20T04:46:39","slug":"overloading-va-overriding-trong-oop","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2010\/08\/20\/overloading-va-overriding-trong-oop\/","title":{"rendered":"Overloading v\u00e0 Overriding trong\u00a0OOP"},"content":{"rendered":"<p>Nhi\u1ec1u l\u1ea7n nh\u1ea7m l\u1eabn c\u00e1i n\u00e0y n\u00ean t\u00f4i b\u00e8n l\u1eadp ra m\u1ed9t trang \u0111\u1ec3 ghi l\u1ea1i s\u1ef1 kh\u00e1c bi\u1ec7t c\u01a1 b\u1ea3n c\u1ee7a hai anh n\u00e0y.<\/p>\n<p><strong>Overloading: <\/strong><\/p>\n<p>D\u1ecbch ra ti\u1ebfng Vi\u1ec7t ngh\u0129a theo ngh\u0129a chuy\u00ean ng\u00e0nh l\u00e0\u00a0 qu\u00e1 t\u1ea3i l\u00ean c\u00e1i \u0111\u00e3 c\u00f3 (nghe bu\u1ed3n c\u01b0\u1eddi qu\u00e1). Th\u1eadt v\u1eady, trong OOP (Object-Oriented Programming) ch\u00fang ta c\u00f3 th\u1ec3 khai b\u00e1o c\u00e1c ph\u01b0\u01a1ng th\u1ee9c (h\u00e0m) c\u00f3 c\u00f9ng t\u00ean nh\u01b0ng kh\u00e1c nhau v\u1ec1 ki\u1ec3u c\u1ee7a c\u00e1c \u0111\u1ed1i s\u1ed1 (arguments), ki\u1ec3u tr\u1ea3 v\u1ec1 c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c v\u00e0 c\u1ea3 s\u1ed1 l\u01b0\u1ee3ng \u0111\u1ed1i s\u1ed1 c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c.<\/p>\n<p>M\u1ed9t v\u00ed d\u1ee5 minh h\u1ecda nh\u01b0 sau:<\/p>\n<p>Gi\u1ea3 s\u1eed ch\u00fang ta c\u00f3 m\u1ed9t l\u1edbp \u0111\u1ecbnh ngh\u0129a ph\u01b0\u01a1ng th\u1ee9c C\u1ed9ng:<\/p>\n<pre>public class Demo {\n  public int Cong(int x, int y) {\n    return x + y;\n  }\n\n  public string Cong(string s1, string s2) {\n    return s1.Concat(s2);\n  }\n}<\/pre>\n<p>Hai ph\u01b0\u01a1ng th\u1ee9c <strong>Cong<\/strong> \u0111\u1ecbnh ngh\u0129a trong c\u00f9ng l\u1edbp l\u00e0 \u0111\u1eb7c \u0111i\u1ec3m kh\u00e1c bi\u1ec7t th\u1ee9 hai gi\u1eefa Overloading v\u00e0 Overriding.<\/p>\n<p><strong>Overriding:<\/strong><\/p>\n<p>N\u1ebfu t\u00e1ch hai t\u1eeb n\u00e0y ra th\u00ec hi\u1ec3u ngh\u0129a r\u00f5 r\u00e0ng h\u01a1n. D\u1ecbch s\u00e1t theo ti\u1ebfng Vi\u1ec7t l\u00e0 che ph\u1ee7 l\u00ean c\u00e1i \u0111\u00e3 c\u00f3 (nghe l\u1ea1 l\u1ea1 n\u1eefa). Trong OOP, c\u00e1c ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 overriding n\u1ebfu ch\u00fang c\u00f3 c\u00f9ng \u0111\u1ed1i s\u1ed1 (c\u1ea3 v\u1ec1 ki\u1ec3u l\u1eabn s\u1ed1 l\u01b0\u1ee3ng) nh\u01b0ng \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a \u1edf c\u00e1c l\u1edbp kh\u00e1c nhau. C\u1ee5 th\u1ec3: m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ecbnh ngh\u0129a \u1edf l\u1edbp c\u01a1 s\u1edf (base class) v\u00e0 ph\u01b0\u01a1ng th\u1ee9c kia \u0111\u1ecbnh ngh\u0129a \u1edf l\u1edbp d\u1eabn xu\u1ea5t (derived class).<\/p>\n<p>Ch\u00fang ta xem x\u00e9t v\u00ed d\u1ee5 sau:<\/p>\n<pre>class Animal{\n   public void move(){\n      System.out.println(\"Animals can move\");\n   }\n}\n\nclass Dog extends Animal{\n   public void move(){\n      super.move(); \/\/ invokes the super class method\n      System.out.println(\"Dogs can walk and run\");\n   }\n}\n\npublic class TestDog{\n   public static void main(String args[]){\n      Animal b = new Dog(); \/\/ Animal reference but Dog object\n      b.move();\/\/Runs the method in Dog class\n   }\n}<\/pre>\n<p>M\u1ed9t s\u1ed1 quy t\u1eafc s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c overriding:<\/p>\n<ul>\n<li>Danh s\u00e1ch \u0111\u1ed1i s\u1ed1 n\u00ean gi\u1ed1ng ho\u00e0n to\u00e0n v\u1edbi ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c overriden.<\/li>\n<li>Ki\u1ec3u tr\u1ea3 v\u1ec1 n\u00ean gi\u1ed1ng (ho\u1eb7c l\u00e0 ki\u1ec3u con) v\u1edbi ki\u1ec3u tr\u1ea3 v\u1ec1 c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c overriden \u0111\u01b0\u1ee3c m\u00f4 t\u1ea3 trong l\u1edbp c\u01a1 s\u1edf.<\/li>\n<li>M\u1ee9c \u0111\u1ed9 truy c\u1eadp b\u1ecb h\u1ea1n ch\u1ebf h\u01a1n nhi\u1ec1u so v\u1edbi ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c overriden \u1edf l\u1edbp c\u01a1 s\u1edf.<\/li>\n<li>C\u00e1c ph\u01b0\u01a1ng th\u1ee9c instance ch\u1ec9 c\u00f3 th\u1ec3 overriden n\u1ebfu ch\u00fang k\u1ebf th\u1eeba t\u1eeb l\u1edbp c\u01a1 s\u1edf.<\/li>\n<li>C\u00e1c ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c m\u00f4 t\u1ea3 final (trong java) kh\u00f4ng \u0111\u01b0\u1ee3c overriden.<\/li>\n<li>C\u00e1c ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c m\u00f4 t\u1ea3 static th\u00ec kh\u00f4ng overriden nh\u01b0ng \u0111\u01b0\u1ee3c m\u00f4 t\u1ea3 l\u1ea1i.<\/li>\n<li>C\u00e1c ph\u01b0\u01a1ng th\u1ee9c kh\u00f4ng k\u1ebf th\u1eeba s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c overriden (hi\u1ec3n nhi\u00ean)<\/li>\n<li>Trong m\u1ed9t l\u1edbp d\u1eabn xu\u1ea5t n\u1eb1m c\u00f9ng g\u00f3i v\u1edbi l\u1edbp c\u01a1 s\u1edf c\u1ee7a c\u00e1c th\u1ec3 hi\u1ec7n (instance), ch\u00fang ta c\u00f3 th\u1ec3 override t\u1ea5t c\u1ea3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a l\u1edbp cha m\u00e0 kh\u00f4ng \u0111\u01b0\u1ee3c khai b\u00e1o final ho\u1eb7c private.<\/li>\n<li>\u0110\u1ed1i v\u1edbi c\u00e1c l\u1edbp d\u1eabn xu\u1ea5t n\u1eb1m kh\u00e1c g\u00f3i v\u1edbi l\u1edbp c\u01a1 s\u1edf, ch\u1ec9 c\u00f3 th\u1ec3 override c\u00e1c ph\u01b0\u01a1ng th\u1ee9c kh\u00f4ng ph\u1ea3i final \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 public ho\u1eb7c protected.<\/li>\n<li>C\u00e1c h\u00e0m t\u1ea1o kh\u00f4ng th\u1ec3 overriden.<\/li>\n<\/ul>\n<p>M\u1ed9t s\u1ed1 b\u00e0i b\u00e1o th\u1ea3o lu\u1eadn r\u1eb1ng ch\u00fang ta c\u00f3 th\u1ec3 hi\u1ec3u overloading l\u00e0 static (easy binding) polymorphism v\u00e0 overriding l\u00e0 dynamic (late binding) polymorphism.<\/p>\n<p>T\u00f3m l\u1ea1i:<\/p>\n<p><strong>Overloading: <\/strong>Methods name same but signatures is different in the class.<\/p>\n<p><strong>Overriding:<\/strong>To redefine the base class method in the derived class is called overriding.<\/p>\n<p>static binding: compile time binding<\/p>\n<p>dynamic binding: run time binding<\/p>\n<p>signatures: arguments<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nhi\u1ec1u l\u1ea7n nh\u1ea7m l\u1eabn c\u00e1i n\u00e0y n\u00ean t\u00f4i b\u00e8n l\u1eadp ra m\u1ed9t trang \u0111\u1ec3 ghi l\u1ea1i s\u1ef1 kh\u00e1c bi\u1ec7t c\u01a1 b\u1ea3n c\u1ee7a hai anh n\u00e0y. Overloading: D\u1ecbch ra ti\u1ebfng Vi\u1ec7t&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[82,85,86],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-lap-trinh-huong-doi-tuong","tag-oop","tag-overloading","tag-overriding"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":0,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}