GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Java Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Inheritance in Java - SyntaxDB - Java Syntax Reference

    Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Package
    • Variables
      • Primitive Data Types
      • Type Casting
      • Variable Declaration
      • Constants
      • Arrays
      • HashMaps
      • HashSets
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • For Each
      • While Loop
      • Do-While Loop
      • Try Catch Block
    • Object Oriented Programming
      • Class Declaration
      • Access Modifiers
      • Constructor
      • Instantiation
      • Class Variables
      • Standard Methods
      • Class Methods
      • Inheritance
      • Interfaces
      • Abstract Methods and Classes
      • Anonymous Class
      • Declaring an Exception
      • Throwing an Exception
    • String Class
      • String Variables
      • String Length
      • String Compare
      • String Trim
      • Split String
      • Substring
      • String Representation
Categories
Language
Java
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: SE 8

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Package
  • Variables
    • Primitive Data Types
    • Type Casting
    • Variable Declaration
    • Constants
    • Arrays
    • HashMaps
    • HashSets
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • For Each
    • While Loop
    • Do-While Loop
    • Try Catch Block
  • Object Oriented Programming
    • Class Declaration
    • Access Modifiers
    • Constructor
    • Instantiation
    • Class Variables
    • Standard Methods
    • Class Methods
    • Inheritance
    • Interfaces
    • Abstract Methods and Classes
    • Anonymous Class
    • Declaring an Exception
    • Throwing an Exception
  • String Class
    • String Variables
    • String Length
    • String Compare
    • String Trim
    • Split String
    • Substring
    • String Representation

Object Oriented Programming

Inheritance in Java

Inheritance is used to inherit another class' members, including fields and methods. A child class extends a parent class' members.


Syntax
modifier class className extends superClass {
    //to access a superClass member
    super.methodName();

    //to override a parent method with the same name
    @Override
    public parentMethodName(params){
    }
} ;

Notes

A child class inherits all members from the super class without direct implementation within the child class. If a method is written in the child class with the same name and parameters as one in the parent class, the parent class' method is overwritten. It is good practice to use the @Override annotation when overriding the parent method.

With class extension, when referring specifically to a super class member, use the keyword 'super'.


Example
public class Cat {
    string name;    
    public Cat() {
        this.name = "Garfield";        
    }    
    public String sound(){ //implemented sound method from the interface
        return "Meow!";
    }
} 

public class Tiger extends Cat { //the variable 'name' and the method 'sound()' are inherited
    public Tiger(){
        super(); //calling the constructor in the super class, Cat
    }

    @Override
    public String sound(){ //overwriting the sound() method in Cat
        return "Roar!";
    } 

    //This method is only for demonstrative purposes and is bad practice (you shouldn't override a method and call super elsewhere)
    public String catSound(){ 
        return super.sound(); //will call the sound method from the Cat parent class    
    }
}

See Also
Documentation

Java Tutorials - Inheritance

Add to Slack

< Class Methods   |   Interfaces >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.