{"id":9572,"date":"2023-09-24T21:31:02","date_gmt":"2023-09-24T21:31:02","guid":{"rendered":"https:\/\/adelnasim.com\/docs\/courses\/object-oriented-programming-in-java\/constructor-no-arg-parameterized-default\/"},"modified":"2024-04-07T01:19:59","modified_gmt":"2024-04-07T01:19:59","slug":"constructor-no-arg-parameterized-default-9572","status":"publish","type":"docs","link":"https:\/\/adelnasim.com\/ar\/docs\/courses\/object-oriented-programming-in-java\/constructor-no-arg-parameterized-default-9572\/","title":{"rendered":"\u0627\u0644\u0640Constructor \u0648\u0623\u0646\u0648\u0627\u0639\u0647  (No-Arg, Parameterized, Default)"},"content":{"rendered":"<h1>\u0645\u0627 \u0647\u0648 \u0627\u0644\u0640Constructor\u061f<\/h1>\n<ul>\n<li>\u0625\u0646\u0647\u0627 \u0637\u0631\u064a\u0642\u0629 \u062e\u0627\u0635\u0629 \u0641\u064a \u0641\u0626\u0629 Java \u064a\u062a\u0645 \u0627\u0633\u062a\u062f\u0639\u0627\u0624\u0647\u0627 \u062a\u0644\u0642\u0627\u0626\u064a\u064b\u0627 \u0639\u0646\u062f \u0625\u0646\u0634\u0627\u0621 \u0643\u0627\u0626\u0646 \u062c\u062f\u064a\u062f \u0645\u0646 \u062a\u0644\u0643 \u0627\u0644\u0641\u0626\u0629.<\/li>\n<li>\u0648\u0627\u0644\u063a\u0631\u0636 \u0627\u0644\u0623\u0633\u0627\u0633\u064a \u0645\u0646\u0647 \u0647\u0648 \u062a\u0647\u064a\u0626\u0629 \u0633\u0645\u0627\u062a \u0627\u0644\u0643\u0627\u0626\u0646 (\u0627\u0644\u062d\u0642\u0648\u0644 \u0623\u0648 \u0627\u0644\u0645\u062a\u063a\u064a\u0631\u0627\u062a) \u0644\u0644\u0642\u064a\u0645 \u0627\u0644\u0623\u0648\u0644\u064a\u0629 \u0627\u0644\u0645\u0646\u0627\u0633\u0628\u0629.<\/li>\n<li>\u0641\u0647\u0648 \u064a\u0636\u0645\u0646 \u0625\u0646\u0634\u0627\u0621 \u0627\u0644\u0643\u0627\u0626\u0646\u0627\u062a \u0641\u064a \u062d\u0627\u0644\u0629 \u0635\u0627\u0644\u062d\u0629 \u0648\u0645\u062a\u0633\u0642\u0629.<\/li>\n<\/ul>\n<h1>\u0642\u0648\u0627\u0639\u062f \u0625\u0646\u0634\u0627\u0621 Constructors:<\/h1>\n<ol>\n<li>\u0646\u0641\u0633 \u0627\u0633\u0645 \u0627\u0644\u0641\u0626\u0629: \u064a\u062c\u0628 \u0623\u0646 \u064a\u0643\u0648\u0646 \u0627\u0633\u0645 \u0627\u0644\u0645\u0646\u0634\u0626 \u0645\u0637\u0627\u0628\u0642\u064b\u0627 \u062a\u0645\u0627\u0645\u064b\u0627 \u0644\u0627\u0633\u0645 \u0627\u0644\u0641\u0626\u0629.<\/li>\n<li>\u0644\u0627 \u064a\u0648\u062c\u062f \u0646\u0648\u0639 \u0625\u0631\u062c\u0627\u0639 \u0635\u0631\u064a\u062d: \u0644\u064a\u0633 \u0644\u062f\u0649 \u0627\u0644\u0645\u0646\u0634\u0626\u064a\u0646 \u0646\u0648\u0639 \u0625\u0631\u062c\u0627\u0639\u060c \u0648\u0644\u0627 \u062d\u062a\u0649 \u0628\u0627\u0637\u0644.<\/li>\n<li>\u0644\u0627 \u064a\u0645\u0643\u0646 \u0623\u0646 \u062a\u062d\u062a\u0648\u064a \u0639\u0644\u0649 Modifiers: \u0644\u0627 \u064a\u0645\u0643\u0646 \u0627\u0644\u0625\u0639\u0644\u0627\u0646 \u0639\u0646 \u0627\u0644\u0645\u0646\u0634\u0626\u0627\u062a \u0639\u0644\u0649 \u0623\u0646\u0647\u0627 \u0645\u062c\u0631\u062f\u0629 \u0623\u0648 \u062b\u0627\u0628\u062a\u0629 \u0623\u0648 \u0646\u0647\u0627\u0626\u064a\u0629 \u0623\u0648 \u0645\u062a\u0632\u0627\u0645\u0646\u0629.<\/li>\n<\/ol>\n<h1>\u0623\u0646\u0648\u0627\u0639 \u0627\u0644\u0640Constructor:<\/h1>\n<ol>\n<li>No-Arg Constructor: \u0645\u064f\u0646\u0634\u0626 \u0644\u0627 \u064a\u0642\u0628\u0644 \u0623\u064a arguments.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Product {\r\n    private String name;\r\n    private String description;\r\n    private float price;\r\n    private int quantity;\r\n    private float discount;\r\n\r\n    public Product() {\r\n        this.name = \"No name\";\r\n        this.description = \"No description\";\r\n        this.price = 0.0f;\r\n        this.quantity = 0;\r\n        this.discount = 0;\r\n    }\r\n}<\/pre>\n<\/li>\n<li>Parameterized Constructor: \u0645\u064f\u0646\u0634\u0626 \u064a\u0642\u0628\u0644 arguments.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public Product(String n, String d, float p, int q, float dis) {\r\n        this.name = n;\r\n        this.description = d;\r\n        this.price = p;\r\n        this.quantity = q;\r\n        this.discount = dis;\r\n    }<\/pre>\n<\/li>\n<li>Default Constructor: \u0627\u0644\u0645\u064f\u0646\u0634\u0626 \u0627\u0644\u0630\u064a \u064a\u062a\u0645 \u0625\u0646\u0634\u0627\u0624\u0647 \u062a\u0644\u0642\u0627\u0626\u064a\u064b\u0627 \u0628\u0648\u0627\u0633\u0637\u0629 \u0645\u062a\u0631\u062c\u0645 Java \u0625\u0630\u0627 \u0644\u0645 \u064a\u062a\u0645 \u062a\u0639\u0631\u064a\u0641\u0647 \u0628\u0634\u0643\u0644 \u0635\u0631\u064a\u062d.<\/li>\n<\/ol>\n<h1>Constructor Chaining<\/h1>\n<p>\u062a\u0633\u0644\u0633\u0644 \u0627\u0644\u0645\u064f\u0646\u0634\u0626: \u0639\u0646\u062f\u0645\u0627 \u064a\u0633\u062a\u062f\u0639\u064a \u0627\u0644\u0645\u064f\u0646\u0634\u0626 \u0645\u064f\u0646\u0634\u0626\u064b\u0627 \u0622\u062e\u0631 \u0645\u0646 \u0646\u0641\u0633 \u0627\u0644\u0641\u0626\u0629\u060c \u064a\u064f\u0633\u0645\u0649 \u0630\u0644\u0643 \u062a\u0633\u0644\u0633\u0644 \u0627\u0644\u0645\u064f\u0646\u0634\u0626.<\/p>\n<h1>\u0627\u0644\u0643\u0648\u062f:<\/h1>\n<p>Product class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Product {\r\n    private String name;\r\n    private String description;\r\n    private float price;\r\n    private int quantity;\r\n    private float discount;\r\n    private String color;\r\n    \r\n    public Product() {\r\n        this.name = \"No name\";\r\n        this.description = \"No description\";\r\n        this.price = 0.0f;\r\n        this.quantity = 0;\r\n        this.discount = 0;\r\n    }\r\n    \r\n    public Product(String n, String d, float p, int q, float dis) {\r\n        this.name = n;\r\n        this.description = d;\r\n        this.price = p;\r\n        this.quantity = q;\r\n        this.discount = dis;\r\n        System.out.println(\"constructor: 5\");\r\n    }\r\n    \r\n    public Product(String n, String d, float p, int q, float dis, String c) {\r\n        this (n,d,p,q,dis);\r\n        this.color = c;\r\n        System.out.println(\"constructor: 6\");\r\n    }\r\n    \r\n    public void display() {\r\n        System.out.println(\"Name = \" + name);\r\n        System.out.println(\"description = \" + description);\r\n        System.out.println(\"Price = \" + price);\r\n        System.out.println(\"Quantity = \" + quantity);\r\n        System.out.println(\"Discount = \" + Discount);\r\n    }\r\n}<\/pre>\n<p>\u0635\u0641\u0627\u062a:<\/p>\n<ul>\n<li>name: \u0646\u0635 \u064a\u0645\u062b\u0644 \u0627\u0633\u0645 \u0627\u0644\u0645\u0646\u062a\u062c.<\/li>\n<li>description: \u0646\u0635 \u064a\u0635\u0641 \u0627\u0644\u0645\u0646\u062a\u062c.<\/li>\n<li>price: \u0641\u0627\u0635\u0644\u0629 \u0639\u0634\u0631\u064a\u0629 \u062a\u062e\u0632\u0646 \u0633\u0639\u0631 \u0627\u0644\u0645\u0646\u062a\u062c.<\/li>\n<li>quantity: \u0639\u062f\u062f \u0635\u062d\u064a\u062d \u064a\u0645\u062b\u0644 \u0643\u0645\u064a\u0629 \u0627\u0644\u0645\u0646\u062a\u062c.<\/li>\n<li>discount: \u0641\u0627\u0635\u0644\u0629 \u0639\u0634\u0631\u064a\u0629 \u062a\u0645\u062b\u0644 \u0646\u0633\u0628\u0629 \u0627\u0644\u062e\u0635\u0645.<\/li>\n<li>color: \u0633\u0644\u0633\u0644\u0629 (\u0627\u062e\u062a\u064a\u0627\u0631\u064a\u0629) \u0644\u0644\u0648\u0646 \u0627\u0644\u0645\u0646\u062a\u062c (\u062a\u0645\u062a \u0625\u0636\u0627\u0641\u062a\u0647\u0627 \u0641\u064a \u0627\u0644\u0645\u064f\u0646\u0634\u0626 \u0627\u0644\u062b\u0627\u0644\u062b).<\/li>\n<\/ul>\n<p>Constructors:<\/p>\n<ul>\n<li>Product() (\u0627\u0644\u0645\u0646\u0634\u0626 \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a): \u062a\u0647\u064a\u0626\u0629 \u062c\u0645\u064a\u0639 \u0627\u0644\u0633\u0645\u0627\u062a \u0628\u0627\u0644\u0642\u064a\u0645 \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a\u0629 (\u201cNo name\u201d, \u201cNo description\u201d\u060c \u0648\u0645\u0627 \u0625\u0644\u0649 \u0630\u0644\u0643).<\/li>\n<li>\u0627\u0644\u0645\u0646\u062a\u062c (String n\u060c String d\u060c float p\u060c int q\u060c float dis): \u0645\u064f\u0646\u0634\u0626 \u0630\u0648 \u0645\u0639\u0644\u0645\u0627\u062a \u064a\u0623\u062e\u0630 \u0648\u0633\u064a\u0637\u0627\u062a \u0644\u0644\u0627\u0633\u0645 \u0648\u0627\u0644\u0648\u0635\u0641 \u0648\u0627\u0644\u0633\u0639\u0631 \u0648\u0627\u0644\u0643\u0645\u064a\u0629 \u0648\u0627\u0644\u062e\u0635\u0645. \u064a\u0637\u0628\u0639 \"constructor: 5\" \u0639\u0646\u062f \u0627\u0644\u0627\u0633\u062a\u062f\u0639\u0627\u0621.<\/li>\n<li>\u0627\u0644\u0645\u0646\u062a\u062c (String n\u060c String d\u060c float p\u060c int q\u060c float dis\u060c String c): \u0645\u064f\u0646\u0634\u0626 \u0630\u0648 \u0645\u0639\u0644\u0645\u0627\u062a \u064a\u0623\u062e\u0630 \u0627\u0644\u0648\u0633\u0627\u0626\u0637 \u0644\u062c\u0645\u064a\u0639 \u0627\u0644\u0633\u0645\u0627\u062a\u060c \u0628\u0645\u0627 \u0641\u064a \u0630\u0644\u0643 \u0627\u0644\u0644\u0648\u0646. \u064a\u0633\u062a\u062f\u0639\u064a \u0627\u0644\u0645\u064f\u0646\u0634\u0626 \u0627\u0644\u062b\u0627\u0646\u064a \u062f\u0627\u062e\u0644\u064a\u064b\u0627 (\u201cconstructor: 5\u201d) \u062b\u0645 \u064a\u0642\u0648\u0645 \u0628\u062a\u0639\u064a\u064a\u0646 \u0627\u0644\u0644\u0648\u0646. \u064a\u0637\u0628\u0639 \"constructor: 6\" \u0639\u0646\u062f \u0627\u0644\u0627\u0633\u062a\u062f\u0639\u0627\u0621.<\/li>\n<li>display(): \u0637\u0628\u0627\u0639\u0629 \u0645\u0639\u0644\u0648\u0645\u0627\u062a \u0627\u0644\u0645\u0646\u062a\u062c \u0628\u062a\u0646\u0633\u064a\u0642 \u0633\u0647\u0644 \u0627\u0644\u0627\u0633\u062a\u062e\u062f\u0627\u0645.<\/li>\n<\/ul>\n<p>Main Class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Main\r\n{\r\n    public static void main(String args[]) {\r\n        Product p1 = new Product(\"Camera\", \"Auto focus\", 99, 10, 5, \"red\");\r\n        Product p2 = new Product();\r\n        p.display();\r\n\r\n    }\r\n}<\/pre>\n<p>\u064a\u0642\u0648\u0645 \u0628\u0625\u0646\u0634\u0627\u0621 \u0643\u0627\u0626\u0646\u064a\u0646:<\/p>\n<ul>\n<li>p1 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0627\u0644\u0645\u064f\u0646\u0634\u0626 \u0627\u0644\u062b\u0627\u0644\u062b\u060c \u0645\u0645\u0627 \u064a\u0648\u0641\u0631 \u0642\u064a\u0645\u064b\u0627 \u0644\u062c\u0645\u064a\u0639 \u0627\u0644\u0633\u0645\u0627\u062a.<\/li>\n<li>p2 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0627\u0644\u0645\u064f\u0646\u0634\u0626 \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a (\u0644\u0645 \u064a\u062a\u0645 \u062a\u0648\u0641\u064a\u0631 \u0648\u0633\u0627\u0626\u0637).<\/li>\n<\/ul>\n<p>\u064a\u062d\u0627\u0648\u0644 \u0627\u0633\u062a\u062f\u0639\u0627\u0621 display() \u0639\u0644\u0649 \u0627\u0644\u0645\u062a\u063a\u064a\u0631 \u0627\u0644\u0645\u0634\u0627\u0631 \u0625\u0644\u064a\u0647 \u0628\u0634\u0643\u0644 \u063a\u064a\u0631 \u0635\u062d\u064a\u062d p (\u064a\u062c\u0628 \u0623\u0646 \u064a\u0643\u0648\u0646 p2) \u0644\u0639\u0631\u0636 \u0627\u0644\u0642\u064a\u0645 \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a\u0629.<\/p>","protected":false},"excerpt":{"rendered":"<p>What is a Constructor? It&#8217;s a special method in a Java class that&#8217;s automatically invoked when a new object of that class is created. Its primary purpose is to initialize the object&#8217;s attributes (fields or variables) to appropriate initial values. It ensures objects are created in a valid and consistent state. Rules for Creating Constructors: [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"parent":9433,"menu_order":72,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-9572","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/9572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/comments?post=9572"}],"version-history":[{"count":6,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/9572\/revisions"}],"predecessor-version":[{"id":10645,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/9572\/revisions\/10645"}],"up":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/9433"}],"wp:attachment":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/media?parent=9572"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/doc_tag?post=9572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}