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 6 of 6

Thread: Beginner's question about inheritance

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Beginner's question about inheritance

    Hello all, I have just began to read the book "Thinking in Java" by Bruce Eckel, and I've got a question about inheritance.

    It is said in book that

    the derived class may have additional or modified methods comparing to the base class;
    the derived class may have additional data comparing to the base class.

    I am just wondering, is it possible that the derived class has "modified data" comparing to the base class? Because the asymmetry between the "method" and "data" at this point makes me uncomfortable.

    Thanks for your attention!


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Beginner's question about inheritance

    Yes the derived class may have its own set of methods and fields. In the simplest form, imagine a derived class any an ordinary class you make, but one which has to also have the parts of the base class.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Beginner's question about inheritance

    This is a tricky question.
    If you look at this example Code:
    public class Vehicle {
     
    	protected int speed;
     
    	public int getSpeed() {
    		return speed;
    	}
     
    }
    public class Car extends Vehicle {
     
    	protected int speed;
     
    	public Car(int speed) {
    		this.speed = speed;
    	}
     
    }

    Then you might see, that the car and the vehicle classes both have an attribute called "speed".
    But the speed of the car is not a modification of the speed of the vehicle.
    A sub-class (in this example the car) can only add new attributes, but neither remove, nor change existing ones from the super class.

    Objects of the Car class have 2 attributes which are both called speed.

    If you create a car from that example like this:
    		Car c = new Car(150);
    		System.out.println("Speed of car: "+c.getSpeed());
    The printed speed will be 0.

    That is because the car only changes its own speed attribute. The speed of the underlying vehicle class was not changed in the constructor.



    What the programmer of that example really should have written is:
    public class Car extends Vehicle {
     
    	public Car(int speed) {
    		super.speed = speed;
    	}
     
    }
    Because this way the value of the speed attribute of the super class was modified.
    And now the Car only has 1 attribute called speed, the one returned by the getSpeed() method.

  4. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    GregBrannon (June 28th, 2014), waltforme (July 7th, 2014)

  5. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner's question about inheritance

    So it is indeed asymmetric. That's fine, I'll try to get use to this.

    But absolutely, thanks for your detailed explanation and your code!

    P.S.

    To my understanding, the first two sections of your code are actually adding a new attribute (although with the same name) to the derived class. Am I right?
    Last edited by waltforme; July 7th, 2014 at 04:32 PM. Reason: One more question

  6. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Beginner's question about inheritance

    Quote Originally Posted by waltforme View Post
    To my understanding, the first two sections of your code are actually adding a new attribute (although with the same name) to the derived class. Am I right?
    Yes exactly. The new class Car will have 2 different attributes which are both called speed.

  7. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner's question about inheritance

    Thanks Cornix!

Similar Threads

  1. question on inheritance
    By antoni in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 10th, 2013, 09:56 PM
  2. [SOLVED] Java Inheritance Question...
    By tim8w in forum Object Oriented Programming
    Replies: 3
    Last Post: October 22nd, 2013, 09:35 PM
  3. Inheritance question!
    By Scorks in forum Object Oriented Programming
    Replies: 4
    Last Post: September 23rd, 2013, 12:56 PM
  4. Inheritance question
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: February 24th, 2012, 07:19 AM
  5. beginner's question
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2010, 04:02 PM

Tags for this Thread