Inheritance

What is inheritance?

Inheritance is a process which allows inheriting the methods and properties of parent or super class.

OR

Deriving an object from an existing class (super class)

Why inheritance?

  • In order to implement tree like structure
  • To implement parent child relationship
  • It makes possible the reusability of already existing class
  • It makes possible for a child to inherit all the properties of parents

[inline:Inheritance.jpg=Inheritance]

In inheritance we create general class that has common set of related items.
So the general class can then be inherited base or child classes.

  • Important
We will never change the original class but if we want to create some functions we will create derive classes.

Super class

A class that is inherited is called as super class or parent class.

These are professionally and pretest classes that are designed by professionals. We check our requirements, if they are what we are going to implement then we derive from them.

Sub Class

A class that does the inheriting is called sub class or child class.

So, a sub class or child class is a specialized version of super class.

How to inherit a class

If we want to inherit a class, we simply use extends keyword. The general form of inheritance would be:

class subclass-name extends superclass-name { /*body of class*/ }

Unlike C and C++ we can specify only one super class, because java does not support direct multiple inheritance. But indirectly we can implement multiple inheritance in java by using interfaces.

Example of inheritance

Below is a simple program that explains the concept of inheritance.

// to create a superclass. class Sample { int a, b; void toshowab() { System.out.println("a and b: " + a + " " + b); } } // to create a subclass class Sample1 extends Sample { int c; void toshowc() { System.out.println("c: " + c); } void sum() { System.out.println("a+b+c: " + (a+b+c)); } }

In our previous discussion, we said that base class can inherit all of the member functions of super class but “it cannot access those members of the super class that have been declared as private.”

So in short, base class cannot have access to private members of super class.

Reference Variable

A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass.

  1. A reference variable is one that does not have its own value, but it refers to another variable for the values.

SuperClass referenceVariable = new SubClass();

Or

SubClass subClassReference = new SubClass();
SuperClass referenceVariable = subClassReference;

Packages

Package is a container”

Packages are container for classes that are used to keep the class name space compartmentalized.

Example:

A package allows us to create a class named List, which we can store in our package without concern that it will collide with some other class named List stored elsewhere.

Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.

  • The package is both a naming and a visibility control mechanism.
  • We can define classes inside a package that are not accessible by code outside that package.
  • We can also define class members that are only exposed to other members of the same package.
  • This allows our classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.

How to define a package

  • The general form of the package statement:

    package pkg;

    Example

    package myPackage;

  • Java uses file system directories to store packages.
  • Example

    The .class files for any classes we declare to be part of myPackage must be stored in a directory called myPackage.

  • The case is significant, and the directory name must match the package name exactly.

Classes

private

Default

protected

Public

Same class

Yes

Yes

Yes

Yes

Same package subclass

No

Yes

Yes

Yes

Same package non-subclass

No

Yes

Yes

Yes

Different package subclass

No

No

Yes

Yes

Different package non-subclass

No

No

No

Yes

Summary of access

Summary of class member access

  • public --> anywhere
  • private --> same class only
  • Default access --> same package only
  • protected --> same package + subclasses

Add new comment