MainMenu

Home Java Overview Maven Tutorials

Sunday, 15 January 2017

Constructor, Super & this keyword in java



For Video Tutorial : Move on Youtube Channel



Note : Select the playlist as per your need & move with number sequence


Constructor in Java with example


For Video : CLICK HERE


A java constructor has the same name as the name of the class to which it belongs. Constructor’s syntax does not include a return type, since constructors never return a value.
Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition.
Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided.
The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.

Constructor Example :


public class cons
{
cons(int a, int b)
{
System.out.println("your sum is :" +(a+b));
}
public static void main(String args[])
{
cons obj = new cons(3,5);
}
}
Output : your sum is :8

For Video : CLICK HERE



Super Keyword



The super keyword in java is a reference variable that is used to refer immediate parent class object.
If parent & child class both have same variable and parent class is inherited by child class(by extends) then only child class variable will be accessible and to access the parent class variable we have to use Super keyword.

Super Keyword Example in Java:


class parent
{
int x =20;
}
public class child extends parent
{
int x = 10;
public void sub()
{
System.out.println("value of x with super keyword : " +super.x);
System.out.println("value of x without super keyword : " +x);
}
public static void main(String args[])
{
child obj = new child();
obj.sub();
}
}
Output :
value of x with super keyword : 20
value of x without super keyword : 10

This Keyword in Java:


This keyword is used to resolve the ambiguity issue between the instance variable and the parameter.

This Keyword Example in Java:


public class cons
{
int x , y, z;
public cons(int x , int t, int z)
{
x = x;
y=t;
this.z = z;
}
public static void main(String args[])
{
cons obj = new cons(3, 50, 40);
System.out.println("i am confuse who is x :" +obj.x);
System.out.println("i know that your are global variable :" +obj.y);
System.out.println("Thanks this, now i know you z :" +obj.z);
}
}
Output :
i am confuse who is x :0
i know that your are global variable :50
Thanks this, now i know you z :40

Tags : What is constructor in java?, what is this keyword in java?, what is super keyword in java


9 comments:

  1. It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge.
    https://www.onlinemediadirect.co.uk/seo-hub/

    ReplyDelete
  2. In addition to the entire typical search engine optimization agency, we develop shareable and PR-helpful campaigns Learn More so that you can roll out all over social media marketing, internet making hyperlinks as well as visibility that increase skilled and gives guests back again to your web page. All of our tasks are maintained by chilly tough figures for that reason there’s no guesswork integrated.

    ReplyDelete
  3. Construction involves workers, transport vehicles, heavy equipment, and other necessary materials like cement bags, hollow blocks, steel rods etc. Construction is a risky work and serious injury or death of workers or damage to property is a common issue. شرکت آرین سازه

    ReplyDelete
  4. Is it time for you again to have the routine checkup for your vehicle? Is your car acting weird and you're worried that the problem may be more serious than what you think? If that's the case and you're living in San Francisco, you are not going to have any problem finding an auto repair shop that will be able to cater to your needs. PTFE Hose

    ReplyDelete
  5. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... https://www.seoexpertindelhi.in/

    ReplyDelete
  6. Commercial real estate is a great option for investing in real estate.Though many only think in terms of residential real estate, there are big (in fact huge) profits to be made in commercial real estate investing. Apartments, rental units, strip malls, retail space, warehouses even vacant land are all commercial investments with huge profit potential. pif by borrower

    ReplyDelete
  7. India has a growing residential handyman industry. The dynamic sector provides splendid career opportunities to job-seekers from different backgrounds. If you are looking for construction jobs, then you must go ahead with the article to learn about your options and opportunities.

    ReplyDelete
  8. โดยอัดน้ำยาฉีดปลวกใต้ดิน ฉีดปลวกทั้งภายใน และภายนอกบ้าน วางเหยื่อล่อปลวก และ วางท่อปลวก พร้อมทั้งให้คำปรึกษาตามความต้องการของลูกค้าได้ทุกรูปแบบ. ประเมินราคาฟรี ไม่มีค่าใช้จ่ายใดๆ รับกำจัดปลวกทั้งจังหวัดเชียงใหม่ ลำพูน ลำปาง เชียงราย พะเยา แพร่ เรารับงานทุกตำบล ทุกอำเภอ ในเชียงใหม่ , อำเภอเมืองเชียงใหม่ , ศรีภูมิ , พระสิงห์ , หายยา , ช้างม่อย , ช้างคลาน , วัดเกต , ช้างเผือก , สุเทพ , แม่เหียะ , ป่าแดด , หนองหอย , ท่าศาลา , หนองป่าครั่ง , ฟ้าฮ่าม , ป่าตัน , สันผีเสื้อ , หางดง , อำเภอแม่ริม , อำเภอสะเมิง , อำเภอสันป่าตอง , อำเภอสันกำแพง , อำเภอสันทราย , สันทรายหลวง , สันทรายน้อย , สันผักหวาน , หนองควาย , บ้านปง , น้ำแพร่ ฉีดปลวกเชียงใหม่ราคา

    ReplyDelete