Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: add a default constructor in abstract class

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post add a default constructor in abstract class

    I have a java code :

            abstract class Fish{
                abstract void Display();
            }
            abstract class FreshWaterFish extends Fish{
            }
            abstract class SaltWaterFish extends Fish{
            }
            class Tombro extends FreshWaterFish{
                @Override
                void Display() {
                    System.out.println("Tombro");
                }
            }
            class Salmon extends SaltWaterFish{
                @Override
                void Display() {
                    System.out.println("Salmon");
                }}
            class Tuna extends SaltWaterFish{
                @Override
                void Display() {
                    System.out.println("Tuna");
            }}
            public class InstanceOfClass {
                public final static int NUMIKAN=4;
                public static void main(String g[]){
                    Fish Fish[] = new Fish[NUMIKAN] ;
                    Fish[0]=new Tombro();
                    Fish[1]=new Tuna();
                    Fish[2]=new Salmon();
                    Fish[3]=new Tombro();
                   for(int i=0; i<NUMIKAN; i++){
                        Fish MyFish = Fish[i];
                        if (MyFish instanceof SaltWaterFish) {
                            MyFish.Display();
            }}}}


    I just want to learn, how should adding a default constructor on FreshWaterFish and SaltWaterFish class, so it displays the output:


    Fresh Water Fish
    Salt Water Fish
    Salt Water Fish
    Fresh Water Fish
    Tuna
    Salmon



    Thanks guys for your reply !


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: add a default constructor in abstract class

    Quote Originally Posted by Kifli View Post
    I just want to learn, how should adding a default constructor on FreshWaterFish and SaltWaterFish class, so it displays the output:
    Fresh Water Fish
    Salt Water Fish
    Salt Water Fish
    Fresh Water Fish
    Tuna
    Salmon
    //Abstract Subclass of Fish
     
            abstract class FreshWaterFish extends Fish{
     
            	public FreshWaterFish()
            	{
            		System.out.println("FreshWaterFish");
            	}
            }
     
    //Abstract Subclass of Fish
     
            abstract class SaltWaterFish extends Fish{
     
            	public SaltWaterFish()
            	{
            		System.out.println("SaltWaterFish");
            	}
            }

Similar Threads

  1. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 05:42 PM
  2. What's difference between public class and abstract class?
    By Java95 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 24th, 2012, 07:37 AM
  3. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  4. abstract class
    By robinglow in forum Java Theory & Questions
    Replies: 2
    Last Post: August 20th, 2010, 02:36 AM
  5. abstract class
    By robinglow in forum AWT / Java Swing
    Replies: 2
    Last Post: August 20th, 2010, 02:36 AM

Tags for this Thread