• No results found

Hybrid Inheritance:

In document 12 Ip Java Notes 1 (Page 49-53)

DATABASE CONNECTIVITY

5. Hybrid Inheritance:

3. Hierarchical Inheritance: when more than one class is derived from ONE base class.

4. Multi-Level Inheritance: when a subclass is derived from a class that itself is derived from another class.

5. Hybrid Inheritance:

A

B

C

A B

C

A

B C D

A

B

A

B

C

A

B C D

B C D

Syntax:

class <derived-class> extends <base-class>{

---;

System.out.println(“Constructor of class One”);

} }

class Two extends One{

int x,y; //new members of subclass Two(){

System.out.println(“Constructor of class Two”);

} }

Here the class „Two‟ is a sub-class or a derived class of the class „One‟ which is the base or superclass. Note that while extending classes instance variables and methods of super-class also become part of the new sub-class.

Now the question arise, can a sub-class can access all the members of its super-class? the answer depends on the various access-specifiers used for the members of a super-class.

Access Specifiers/Access Control of Inherited Members

Although a sub-class inherits all the members of its super-class, yet it can access only those variables or methods for which it has access permissions. In other words, the access of inherited members depends upon their access modifiers i.e. whether they are private or public or protected.

Modifiers Specification

private Members are accessible only inside their own class where they have been originally declared and nowhere else.

public Members are accessible in all the classes whether a sub-class or class in the same package or class in another class along-with their own class

protected Members are accessible inside their own class as well as in all sub-classes of their class, regardless of whether sub-classes exist in the same package or any other package.

default These are those members that are declared without any access modifiers i.e. only with a data type. These members are accessible only inside those classes that are in the same package.

private protected

These members are accessible only from sub-classes whether in the same package or any other package

Modifier Inside own class

Inside sub-classes Inside non-sub-classes In the same

Things you should know about the Inheritance in JAVA:

 Java allows ONLY single Inheritance and Hierarchical and Multi-Level Inheritance which means that a class has at most one immediate super-class.

 Every class in Java has one and only one immediate/direct super-class, but might have several sub-classes.

 The inheritance relationship is transitive. Therefore, every class inherits variables and methods from all classes which are higher in the class hierarchy (direct or indirect super-class)

Overriding methods and Hiding Member Variables:

Sometimes, a sub-class uses the same name for variables and methods as in the super-class. This leads to the following behaviour:

 Methods are OVERRIDEN

 Variables are HIDDEN class A{

void print1(){

System.out.println(“The class A”);

} }

class B extends A { void print1(){

System.out.println(“The class B”);

} }

Coding behind a BUTTON:

B objB1=new B();

objB1.print1();

In the above example, the „print1‟ method of class B will be executed and Not that of the class A. You can say

„print1()‟ method of class A will be overridden by the „print1()‟ method of class B.

Similarly, variables of super-class are said to be HIDDEN, if the variable with the same name and type exist in the sub-class or derived class.

Thus, a method in a sub-class hides or overshadows a method inherited from the super-class if both methods have the same signature i.e. same name, number and type of arguments, and the same return type. This property is known as Overriding the inherited methods.

You can still refer to the method of the super-class by using the keyword super i.e. super.overridenMethod() refers to the method as it is defined in the super-class.

Similarly, you can refer to the super-classes hidden variable by : super.hiddenVariable

class A{

void print1(){

System.out.println(“The class A”);

} }

class B extends A { void print1(){

System.out.println(“The class B”);

}

void print2(){

super.print1();

}

Coding behind a BUTTON:

B objB1=new B();

objB1.print1(); // print1 of class B will be called

objB1.print2(); // print2 of class B will be called which in turn will call print1 of class A

 NOTE that an object of a sub-class cannot directly access an Overridden method of super-class. it can do so by calling a method of the sub-class which in turn can call the Overridden method of the super-class with the keyword „super‟.

The keyword „final‟:

 Using final keyword with a variable makes it a constant and they cannot be changed in the class and sub-classes.

 Using final keyword with methods will not allow them to be overridden by sub-classes methods.

 Using final keyword with a class will not allow it to be extended or say inherited.

Example to depict the concepts of:

 Overridden Methods

 Hidden Variables

 Accessing Overridden Methods

 Accessing Hidden Variables class superClass {

String id=”id of super-class”;

void print() {

System.out.println(“print method of the super-class”);

} }

class subClass extends superClass {

String id=”id of sub-class”; //overriding super-classes variable id void print() { // overriding super-classes method print()

System.out.println(“print method of the sub-class”);

}

void print2(){

System.out.println(id);

print() ;

System.out.println();

System.out.println(super.id); // accessing the hidden variable of the superclass super.print(); // accessing overridden method of the superclass }

}

The OUTPUT of the method print2() will be:

id of sub-class

print method of the sub-class id of super-class

print method of the super-class

Method Overloading/Function Overloading

A function name having several definitions in the same scope that are differentiated by the number and type of arguments is said to be an overloaded functions or methods.

 The key to Method Overloading is a method‟s argument list which is also known as the function signature. It is the signature and not the function type that enables function overloading.

 A function‟s argument list is known as a function‟s signature.

 Functions with the same name and signature but different return types are not allowed in Java.

 But you can have functions with the same name and different return types ONLY IF their signature is different.

o Same name, different return types and different signatures int sqr(int n) {

return n * n ; } float sqr(float n) {

return n * n ; }

o Same name, same return type and different signatures void sqr(int n) {

System.out.println( n * n ) ; } void sqr(float n) {

System.out.println( n * n ) ; }

o Same name, same signature but different return type are NOT ALLOWED but if the

signatures of two functions differ in either the number or type of their arguments, the two methods are considered to be OVERLOADED

In document 12 Ip Java Notes 1 (Page 49-53)

Related documents