The Artima Developer Community
Sponsored Link

Java Buzz Forum
Constructor chaining in java using this and super Keywords

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Constructor chaining in java using this and super Keywords Posted: Feb 15, 2016 9:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Constructor chaining in java using this and super Keywords
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
Constructor chaining example program in same class using this keyword.


  1. package instanceofjava;
  2. class ConstructorChaining{
  3. int a,b 
  4. ConstructorChaining(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8.  
  9. ConstructorChaining(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. ConstructorChaining(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20.  
  21. public static void main(String[] args){
  22.  
  23.  ConstructorChaining obj=new ConstructorChaining();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2

Constructor chaining example program in same class using this and super keywords.

  1. package instanceofjava;
  2. class SuperClass{
  3.  
  4. SuperClass(){
  5. this(1);
  6.  System.out.println("Super Class no-argument constructor");
  7.  
  8.  
  9. SuperClass(int x ){
  10.  
  11. this(1,"constructor chaining"); 
  12. System.out.println("Super class one -argument constructor(int)");
  13.  
  14. }
  15.  
  16. SuperClass(int x , String str){
  17.  System.out.println("Super class two-argument constructor(int, String)");
  18.  
  19. }


  1. package instanceofjava;
  2. class SubClass extends SuperClass{

  3. SubClass(){
  4. this(1);
  5.  System.out.println("Sub Class no-argument constructor");
  6.  
  7.  
  8. SubClass(int x ){
  9.  
  10. this("Constructor chaining"); 
  11.  System.out.println("Sub class integer argument constructor");
  12.  
  13. }
  14.  
  15. ConstructorChaining(String str){
  16. //here by default super() call will be there so it will call super class constructor
  17.   System.out.println("Sub class String argument constructor");
  18.  
  19. public static void main(String[] args){
  20.  
  21.  SubClass obj=new SubClass();
  22.  
  23.  
  24. }
  25. }

Output:

  1. Super class two-argument constructor(int, String)
  2. Super class one -argument constructor(int)
  3. Super Class no-argument constructor
  4. Sub class String argument constructor
  5. Sub class String argument constructor
  6. Sub class integer argument constructor




Read: Constructor chaining in java using this and super Keywords

Topic: The Best of Both Worlds Previous Topic   Next Topic Topic: Difference between HashSet and TreeSet in Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use