Constructor overloading in java

Constructor overloading in java

 

using System; 

public class CParent 
{ 
	public int PubA; 
	private int PriA; 
	protected int ProA; 
	
	public CParent() 
	{ } 
	
	public CParent(int a, int b, int c) : this() 
	{ 
		this.PriA = a; 
		this.ProA = b; 
		this.PubA = c; 
	} 
} 

public class CChild : CParent 
{ 
	public int pubB; public CChild() //First calls parent class constructor. 
	{ } 

	public CChild(int a, int b, int c, int d) : base(a, b, c) 
	{ 
		//this.PriA = a; 
		this.ProA =b; 
		this.PubA = c; 
		this.pubB = d; 
	} 
	
	public void Foo() 
	{ 
		CParent p = new CParent(); 
		CChild c = new CChild(); 
		ProA = 10; //Allowed 
		c.ProA = 10; //Allowed 
		//p.ProA = 10; //Compilation Error because “p” is of type parent. 
	} 
} 

class Program 
{ 
	static void Main(string[] args) 
	{ 
		CChild b; b = new CChild(1, 2, 3, 4);
		b.Foo(); 
	} 
}
Constructor overloading in java

Constructor overloading in java

Output :

–Center at 5 and 5
Radius = 3
–Center at 10 and 20
Radius = 5
–Center at 15 and 25
Radius = 10

 

Learn Constructor overloading in java with theory and examples.

Advertisements

About akjava2all

Learn java and advance java online with simple java code, java tutorial, java example and java ppt.
This entry was posted in Core Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s