Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Inheritance

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 265
    Comment on it

    Inheritance in java is concept by which one class can have the method of the other class.
    The class that is being inherited is called Base or Parent class and the one which derive is called the Derive or child class.
    Inheritance can be of the two type
    1. Muli level
    2. Multiple
    Multiple Inheritance is not by simply extending the class we use another way to do that.
    Multi level can be done by extending the class name.


    public class Bicycle {
    
        // the Bicycle class has three fields
        public int cadence;
        public int gear;
        public int speed;
    
        // the Bicycle class has one constructor
        public Bicycle(int startCadence, int startSpeed, int startGear) {
            gear = startGear;
            cadence = startCadence;
            speed = startSpeed;
        }
    
        // the Bicycle class has four methods
        public void setCadence(int newValue) {
            cadence = newValue;
        }
    
        public void setGear(int newValue) {
            gear = newValue;
        }
    
        public void applyBrake(int decrement) {
            speed -= decrement;
        }
    
        public void speedUp(int increment) {
            speed += increment;
        }
    
    }
    
    A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:
    
    public class MountainBike extends Bicycle {
    
        // the MountainBike subclass adds one field
        public int seatHeight;
    
        // the MountainBike subclass has one constructor
        public MountainBike(int startHeight,
                            int startCadence,
                            int startSpeed,
                            int startGear) {
            super(startCadence, startSpeed, startGear);
            seatHeight = startHeight;
        }   
    
        // the MountainBike subclass adds one method
        public void setHeight(int newValue) {
            seatHeight = newValue;
        }   
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: