Jump to content

Use Constuctor In Inheritance

- - - - -

  • Please log in to reply
11 replies to this topic

#1
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I've created object named "obj2". STD2 is extended on STD;


// main.java

public class main {	

	public static void main(String[] args ){    

	       STD2 obj2=new STD2();	         		

	}		

}



// STD.java

public class STD{

		   public STD(){ 

			   System.out.println("Works STD Constructor!");

		   }

}   





// STD2.java

public class STD2 extends STD{

    public STD2(){ 

	System.out.println("Works STD2 Constructor!");

	}

}   	  



Si then the result will be:

C:\Users\vakho\Desktop>javac *.java
C:\Users\vakho\Desktop>java main

Works STD Constructor!
Works STD2 Constructor!


Questions:
1)Could I invoke(run) only STD2 Constructor here?... If i Cerate STD2 object, why STD Constuctor runs (Invokes)?
2) In C++, we could Write all the classes together, Could we make something like this in Java?

Edited by VakhoQ, 20 July 2011 - 06:40 AM.

GNU/Linux Is the Best.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
When you don't put super() as first line of your constructor, the Java compiler will do it itself.

The actual code that's executed is:
public class STD2 extends STD{ 

    public STD2(){  

      super();

      System.out.println("Works STD2 Constructor!"); 

    } 

}

Even if your code doesn't extend anything, the code will also look like this, as in the background all classes you make extend the Object class.
That's why you can do toString() .equals() on all objects you create even tho you didn't write those methods. Object has those methods, and those will be used if you call em.

Quote

2) In C++, we could Write all the classes together, Could we make something like this in Java?
Not quite sure what you mean here, can you write a short C++ example?

#3
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
wim DC

In C++, we could write all the classes in one *.cpp file, for example:




class CSquare;

class CRectangle { // First Class

    int width, height;

  public:

    int area (){return (width * height);}

    void convert (CSquare a);

};


class CSquare {   // Second Class

  private:

    int side;

  public:

    void set_side (int a){side=a;}

    friend class CRectangle;

};


void CRectangle::convert (CSquare a) {

  width = a.side;  height = a.side;

}

  

int main () {

  CSquare sqr;

  CRectangle rect;

  sqr.set_side(4);

  rect.convert(sqr);

  cout << rect.area();

  return 0;

}





In Java, I have to make "CSquare.java" , and "CRectangle.jave" . I should write each class to each file (*.java)
GNU/Linux Is the Best.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Writing it in separate .java files is the common way to do it.

Java does support inner classes / nested classes. But then you create a class within a class, and not 2 separate ones like you do in C.
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Nested classes can be somewhat hard to understand, I've seen at least 2 threads about it in the past few months, of which 1 is this http://forum.codecal...html#post305486

#5
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

wim DC said:

Even if your code doesn't extend anything, the code will also look like this, as in the background all classes you make extend the Object class.
That's why you can do toString() .equals() on all objects you create even tho you didn't write those methods. Object has those methods, and those will be used if you call em.

could you give me an example?
GNU/Linux Is the Best.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

public class Example{

  public static void main(String[] args){

    Example example1 = new Example();

    System.out.println(example.toString());


    Example example2 = new Example();

    System.out.println("ex1 vs ex1: " + example1.equals(example1));

    System.out.println("ex1 vs ex2: " + example1.equals(example2));

  }

}

(JAva compiler also creates a no-arg constructor if you didn't create any constructor)

So even tho you didn't write any code for the toString() and .equals(...) methods, Java knows them because the Object class has them, and every class in Java extends Object. Always.

toSTring of Object is just some memory address shizzle, and .equals(..) will also use the memory location to determine if 2 objects are the same.

#7
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

wim DC said:

Writing it in separate .java files is the common way to do it.

Java does support inner classes / nested classes. But then you create a class within a class, and not 2 separate ones like you do in C.
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)


Nested classes can be somewhat hard to understand, I've seen at least 2 threads about it in the past few months, of which 1 is this http://forum.codecal...html#post305486


thanks a lot! :) :) :)
GNU/Linux Is the Best.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Just a side note, you can put all your classes in one file, but when you compile them they will still be split into separate .class files. Given that behavior, most people just keep the .java files separate to mirror what will happen.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

WingedPanther said:

Just a side note, you can put all your classes in one file, but when you compile them they will still be split into separate .class files. Given that behavior, most people just keep the .java files separate to mirror what will happen.


/*### main.java ###*/

public class main {	

	public static void main(String[] args ){ 		

	    Class1 obj=new Class1();		

	    Class2 obj2=new Class2();	    		

	}	

}


do you mean like this?
/*### Allclasses.java ###*/

public class Class1{  // first class

     public Class1(){    	

     System.out.println("class1 Constructor!"); 

     }     

}   		

public class Class2 extends Class1{ // second class	

	   public Class2(){

	   System.out.println("Class2 Constructor!"); 	 

	  }   	   

}   


ERRORS

C:\Users\vakho>cd Desktop

C:\Users\vakho\Desktop>javac *.java


ALLclasses.java:1: class Class1 is public,[COLOR="#B22222"] should be declared in a file named Cl

ass1.java[/COLOR]

public class Class1{

       ^

ALLclasses.java:8: class [COLOR="#B22222"]Class2 is public, should be declared in a file named Cl

ass2.java[/COLOR]

public class Class2 extends Class1{

       ^

2 errors

C:\Users\vakho\Desktop>

or did you mean "Nested Classes"?
GNU/Linux Is the Best.

#10
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
File name must be equal to the public class in the file.

you can write several classes to a same file like this

ClassA.java
public class ClassA{

}

class ClassB{

}

class ClassC{

}

However it's recommended that you write them all to different files - easier to manage. Unless you are doing it in some text redactor. You should use good IDE like Eclipse or Netbeans, when writing Java.

#11
Ritwik I the programmer

Ritwik I the programmer

    Newbie

  • Members
  • PipPip
  • 17 posts
When you write multiple classes in a .java file, it
1)has to have a public main() containing class.
2)has to have same filename as above.
Try using BlueJ, it masks and corrects some errors you face in direct
handling of .java files.

#12
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
"1)has to have a public main() containing class."

Not really. public class won't need main method in it. You just need your main method somewhere to run the program.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users