Call for New Batch / Any Query / Demo Classes : ☎ 08871209400 , 7987028543 , Zone-2,M.P.Nagar,Bhopal

Inheritance in Java

A class that is derived from another class is called a subclass. The class from which the subclass is derived is called a superclass. This process of inheriting properties from another class is called Inheritance. Inheritance works on a simple principle that if required functions are already present in a class then it could be reused. Another benefit is that a few functionalities that are not required can be avoided.

Every class has one and only one direct superclass. In the absence of any other explicit superclass, every class is implicitly a subclass of Object. Though Object itself has no Superclass.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object. A subclass inherits all the members from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Class Hierarchy in Java

The Object class, defined in the java.lang package, defines and implements behavior common to all classes, including the ones that are written by the user. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.

At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.

Sample Program using Inheritance



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;
        }   
    }
    
     

MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug.

Download Source code of Bicycle Class

References : Oracle share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

Explore the Technology World

How to Build an App for the Google AssistantApps for the Google Assistant are the gateway for your users to engage with your services through Google Home, Android phones, iPhones.Watch here to build an as ...Read MoreIntroduction to AMP - Accelerated Mobile PagesAMP, short for Accelerated Mobile Pages, is new new open source initiative to make the mobile web awesome again.Watch Here the Introduction to AMP ...Read MoreHadoop - Just the Basics for Big Data Rookies (Best online tutorial)This Tutorials assumes absolutely no knowledge of Apache Hadoop and will provide a complete introduction to all the major aspects of the Hadoop ecosystem of pro ...Read MoreGoogle Python online tutorial-Interested in learning python? Then you must watch this videoPython is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code.wat ...Read Morelatest product and platform innovations at Google by Sundar PichaiLearn about the latest product and platform innovations at Google in a Keynote led by Sundar Pichai.Video is also subtitled in Chinese, Indonesian, Italian, Jap ...Read MoreFuture of the Java Platform and the JDKJava has been one of the most popular programming platforms in the world for twenty-three years as One Language, One Platform, a Lifetime of Opportunity. How d ...Read More

Call / Visit for New Batch

Services

Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance

Address

  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐 www.myjava.in
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute