Inheritance or Superclass - Subclass Concept in Java

Inheritance work on tree like hierarchical structure. Inheritance is a form of reusability in which new classes are created from current classes by absorbing their attributes and behaviour and arrange these with capacity the new classes require.Inheritance are powerful techniques for dealing with software complexity.


class Subclass-name extends Superclass-name  
   //methods and fields  


class Student{  
 int marks=70;  
public class scienceSubject extends Student{  
 int internalMarks=20;  
 public static void main(String args[]){  
   scienceSubject p=new scienceSubject();  
   System.out.println(" Marks in science :"+p.marks);  
   System.out.println("InternalMarks in science:"+p.internalMarks);  


Marks in science :70
InternalMarks in science:20

Superclass In Java

Superclass is also known as base class or parent class. The subclass inherit all the features of superclass.

when creating a new class, instead of writing completely new instance variable are instance methods, the programmer can enrolled that the new class is to inherit the instance variable and instance method of a already defined superclass.

An indirect superclass is inherited from two or more levels up the class hierarchy.

Superclass objects are not objects of the superclass's subclasses.We will take the advantage of this "subclass-object-is-a superclass object " relationship to perform some powerful control eg:- we can link a wide variety of different objects related to common superclass through  inheritance into a linked list of superclass objects.This allows a variety of objects to be processed in a general way 

Superclass members that should not be accessible  to a subclass via inheritance and declared private in the superclass.

When a superclass members is not suitable for a subclass, that member can be overridden in the subclass  with  a suitable   application.  

Subclass In Java

A subclass are also known as derived class, extended class, or child class. It inherit with the other class.

The new class is directed to as a subclass. Each subclass itself become a candidate to be a superclass(Parent class) for some incoming member of  the subclass.

A subclass normally adds a particular variable and the particular methods of its own so a subclass are  generally larger than its superclass.

With the help of single inheritance , the subclass begin out basically with the same as the parent class

Every object of a subclass is also an object of that subclass's including the property of superclass.

The subclass method and method of other classes in the same package as the superclass  can be access protected  by the superclass members.

A subclass can, however ,access the public, protected and package access members  of its superclass if it is in the same package as the  superclass.

A subclass can result to state change in superclass privatemembers only through public protectedand package access methods provided in the  superclass and inherited into the subclass


(Parent class)

(child class)