constructors

constructors

View Answers

October 29, 2008 at 10:18 AM

Hi friend,


Constructors cannot be inherited, basically a constructor is not a method.Constructors are used to give a valid state for an object at creation. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.


class A{
A(){
/*do something*/
}
}
class B extends A{
B(){
/*do something*/
}
public static void main(String[] args){
A a = new A();
B b = new A();//is'nt this confusing?
}

----------------------------------------

Visit for more information.

http://www.roseindia.net/java/beginners/constructoroverloading.shtml

Thanks.









Related Tutorials/Questions & Answers:
Constructors in Java
Constructors in Java  When do we use Constructors in JAva?   All object creation is done through constructors. If no constructor is specified, the compiler will supply a default empty constructor that basically does
constructors
Advertisements
constructors
How are this() and super() used with constructors?
How are this() and super() used with constructors?   Hi, How are this() and super() used with constructors? thanks
constructors - Java Beginners
constructors  can constructors be inherited???  Hi friend, Constructors cannot be inherited, basically a constructor is not a method.Constructors are used to give a valid state for an object at creation
Constructors .equals method
Constructors .equals method  Hi, my two dates never match using .equals method and two different constructors. o/p is like examples.Pdate@192d342Blah dateexamples.Pdate@6b97fd Code: package examples; public class Pdate
Constructors .equals method
Constructors .equals method  Hi, my two dates never match using .equals method and two different constructors. o/p is like examples.Pdate@192d342Blah dateexamples.Pdate@6b97fd Code: package examples; public class Pdate
Thread Constructors
Thread Constructors       Several constructors are available for creating new Thread instances.    Thread() ADS_TO_REPLACE_1 Thread(String)   Thread
why we use constructors instead of methods in java?
why we use constructors instead of methods in java?   why we use constructors instead of methods in java
Constructors, setters, getters - Java Beginners
Constructors, setters, getters  A bank charges $10 per month plus the following check fees for a commercial checking account: $.10 each for less than 20 checks $.08 each for 20-39 checks $.06 each for 40-59 checks
What are constructors ? explain different types of constructor with example
What are constructors ? explain different types of constructor with example  Hi, What are constructors ? explain different types of constructor... constructor require. That's why the Constructors are used to initialize the instance
Constructors, setters, getters 2 - Java Beginners
Constructors, setters, getters 2  A bank charges $10 per month plus the following check fees for a commercial checking account: $.10 each for less than 20 checks $.08 each for 20-39 checks $.06 each for 40-59 checks
Exceptional Constructors - Java Tutorials
Java Constructors
Java Constructors In this tutorial we will discuss in java constructor and its type with example. Constructors : A constructor is a special kind of method... of constructors-ADS_TO_REPLACE_1 Constructor name must be same as the class
Objective C Constructors
Objective C Constructors       Objective-C enables user to define constructor with the help of self and super keywords. Like java Objective-C has parent class and programmer
Java Constructor Overloading Example
is achieved in the Java programming. Constructors in Java are a special type... class name, it may contain any number of parameters. Constructors defined... programming is called constructor overloading. Constructors with different
Use Constructor in JSP
; This section illustrates you how to use constructors in jsp.ADS_TO_REPLACE_1 Constructors are used to initialize the object. They are just like method declaration but they do not return value. Constructors are defined
Simple date formatter example
are going to describe you how you can use different constructors of SimpleDateFormat class to convert the date into the specified format. There are various constructors
Simple Date example
Simple Date example       In this section we have presented a simple Date example that shows how you can use different constructors of Date. We can construct the Date
Java Date
a simple Date example that shows how you can use different constructors of Date... are going to describe you how you can use different constructors
Constructor Inheritance
;    Constructors are used to create objects from...;   <title>Working With Constructors...;   <h1>Working With Constructors
How to write a file in Java?
. By default, constructors of this class assume that character encoding and byte... OutputStreamWriter on a FileOutputStream. There are various constructors..._TO_REPLACE_2 Some of these constructors are as follows: FileWriter(File file
Java API
, interfaces with their methods, constructors that come with JDK. Java API included
Inheritance
will learn more about Constructor and how constructors are overloaded in Java... in Java. Constructors are used to assign initial values to instance... because JVM differentiates constructors on the basis of arguments passed
Inheritance
features like methods, data members, access controls, constructors, keywords...; int get(int p, int q){   x=p; y=q; ...;   int get(int p, int q){   x=p; y
Java Date
core java question - Java Interview Questions
why we use constructors????? or what is the need of constructors?? or what is the utility of constructors????  to intialize the instance variabe we will use the constructors
Java fundamentals
Java fundamentals  Does a class inherit the constructors of its superclass
Topic 1: Declarations, Initialization and Scoping
java
java  create an interactive java program for displaying student details by using multiple constructors
Constructor in java
When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods
Reflection
, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within
java
java   Why we didnot write constructors in interfaces   interface is like a pure abstract class, though it is not a class at all... be instantiated. Therefore it does not write constructors in interfaces. For more
java - Java Beginners
constructors: public A() { } private A() { } protected A() { } Can you say what is the difference among the above 3 constructors? Why we need put access specifiers to constructors?   hi friend, public
java - Java Beginners
constructors means that you can't instantiate the class from outside the class... to declare constructor as private. Yes we can declare Constructors as private
java - Java Interview Questions
java   how can we create private constructor? what is its significance?  private constructors prevent a class being explicitly instantiated by other classes We can create private default constructors so that other
java
syntax does not include a return type, since constructors never return a value.It...://www.roseindia.net/java/java-tips/oop/constructors/constructor.shtml
java - Java Interview Questions
requests. Why not use a constructor instead? Well, in JDK 1.0 constructors... to declare constructors. This means that the javax.servlet.Servlet interface cannot... constructors for your servlets, but in the constructor you don't have access
How to Solve the Syntax Error in java??
"i" import java.util.Scanner; public class constructors { int i ; private int...[]) { System.out.println("Enter the name:"); } } class constructor{ constructors objectname = new constructors(); Scanner myScanner = new Scanner(System.in
java - Java Beginners
-tips/oop/constructors/constructor.shtml Thanks
Overloading constructor in flex
Overloading constructor in flex  Hi...... What is overloading a constructor? How do you overload constructors in flex? please tell me about it...........ADS_TO_REPLACE_1 Thanks   Hi..... Action script does
How to? - IoC
How to?  I am having problems coding my LOC to count constructors and methods?  Hi , Methods and constructors both use super... is "); super.getBirthInfo(); } } Constructors
Constructor
Constructor  what is the advantage of a constructor ? not the purpose of the constuctor? give me some in-depth analysis?   Please visit the following links: http://www.roseindia.net/java/java-tips/oop/constructors
java
java  can you explain how can you practically do dynamic loading? what is the use of javap tool? what is the use if instanceof keyword? can constructors be parameterized? what is difference between static and non static fields
How display a image on servlet from file upload - JSP-Servlet
, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some
class
medals. In this class, you should also define constructors, and assessor, mutator..., Country[], to store medal records of countries. Define constructors and relevant
Java help
' for Fahrenheit. The class should have Four Constructors: One for the number of degrees.... For each of these constructors, assume zero degrees if no value is specified... the methods. Be sure to invoke each of the constructors, to include at least one
Java Help
' for Fahrenheit. The class should have Four Constructors: One for the number of degrees.... For each of these constructors, assume zero degrees if no value is specified... the methods. Be sure to invoke each of the constructors, to include at least one
The new keyword in java
but intanstiation. The new keyword is followed by a call to constructors... is followed by a class name and passing values to the constructors. Number of arguments to the constructors must be the same in signature of constructor
JAVA programme - Java Interview Questions
different type of constructors. b.Availability of "charAt" function

Ads