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: Two Constructors Accessed?

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Two Constructors Accessed?

    Hello, I was trying to learn about Abstract classes and I came to find the code you see below. My question is, why do you also see the abstract Bike class constructor being executed when creating the Honda object. The Honda class already has its own constructor and yes, it does extend to Bike, but if the Honda already implements the constructor why does the Bike constructor show up in the console?

    abstract class Bike{  
     
    	Bike(){
    		System.out.println("Bike Constructor accessed.");
    	}  
     
    	abstract void run();  
     
    	void changeGear(){
    		System.out.println("gear changed");
    	}  
    } //Bike
     
    class Honda extends Bike{  
     
    	Honda(){
    		System.out.println("Honda Constructor accessed.");
    	}
    	void run(){
    		System.out.println("running safely..");
    	}  
    }  
     
    public class TestingCode {
     
    	public static void main(String[] args) {
     
    		Honda obj = new Honda();  
    		obj.run();  
    		obj.changeGear();
     
    	}//Main
     
    	//Start
     
    }//Class

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Two Constructors Accessed?

    It is probably explained in this doc: https://docs.oracle.com/javase/specs...tml/index.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: February 6th, 2014, 03:42 AM
  2. call() is not public in BTEST, cannot be accessed by outside package
    By ozman26 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 9th, 2012, 07:40 PM
  3. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM