15
Inheritance, Interfaces, Polymorphism, Method Overriding

Learn Java Part 11

Embed Size (px)

Citation preview

Inheritance, Interfaces, Polymorphism, Method Overriding

When you want to create a new class and there is already a class that includes some of thecode that you want, you can derive your new class from the existing class. In doing this, youcan reuse the fields and methods of the existing class without having to write. This isknown as Inheritance.For example:

class A {int x;long y;void showValues( ) {

System.out.print(“x: ”+x+” y”+y);}

void setValues(int a, long b) {x=a;y=b;

}}

Here, B extends A, therefore B inherits all functions and member variables from A

class B extends A {String z;void showString( ) {

System.out.print(“z: ”+z);}

void setString(String s) {z=s;

}}

A class that is derived from another class is called a subclass (also a derived class, extendedclass, or child class). The class from which the subclass is derived is called a superclass (alsoa base class or a parent class).In previous example, class A is superclass (also a base class or a parent class).class B is subclass(also a derived class, extended class, or child class)

A subclass inherits all the members (fields, methods, and nested classes) from itssuperclass. Constructors are not members, so they are not inherited by subclasses, but theconstructor of the superclass can be invoked from the subclass.to refer to superclass we use the keyword: superclass a class b extends a{ {int a=5; int a=10;} void show()

{ System.out.println(“a of child class: “+a);System.out.println(“ a of super class: “+super.a);

}}

output: a of child class: 10a of super class: 5

Same class Other class in same package

Child class in same package

Other class in same package

Child class in other package

private Yes No No No No

default Yes Yes Yes No No

protected Yes Yes Yes No Yes

public Yes Yes Yes Yes Yes

Figure: Accessibility of variable or function when using particular access-specifier

class a{

int a=5;a(int value){

a=value;}

}

class b extends a{

int a=10;void show(){ System.out.println(“a of child class: “+a);

System.out.println(“ a of super class: “+super.a);}b(){

super(0); //call the super class constructor}

}

When super class has parametrised constructor then child class constructor must pass the values tosuper class constructor. Using:super(values);

class a{

private int a=5;a(int value){

a=value;}

}

class b extends a{

int a=10;void show(){ System.out.println(“a of child class: “+a);

System.out.println(“ a of super class: “+super.a); // will give error}b(){

super(0); //call the super class constructor}

}

class a{

int a=5;}

class b extends a{

int b=10;}

class c extends b{

int c=20;}

Nowc obj=new c( );System.out.println(“A: “+a);System.out.println(“B: “+b);System.out.println(“C: “+c);

will output:A: 5B: 10C: 20

• All variables of an interface are public, final and static• All methods of an interface are public and abstract• interface itself is abstract• An interface can only extends other interfaces

Syntax:interface interfaceName{//members}for example:interface MyInterface{

int a=5; //public static final int a=5;void show(); // public abstract void show();

}

classes that implements the interface must override interface’s all methods

class Demo implements MyInterface{

int a=10;

@overridepublic void show() //Must write public{

System.out.println(“A of interface: ”+MyInterface.a); //a is static so refer it using interface nameSystem.out.println(“A of class: ”+a);

}}

class Demo extends AnotherClass implements MyInterface{

int a=10;

@overridepublic void show(){

//statements}

}

Child class reference can be assigned to super class reference. For example:

class A{

int a=5;int b=6;void show( ){ System.out.println(“a: “+a+” b: “+b); }

}

class B extends A{

int c=10;int d=20;void print(){ System.out.println(“c: “+c+” d: “+d); }

}

A ob1=new A( );B ob2=new B( );

By using ob1 we can access:ob1.a;ob1.b;ob1.show();

By using ob2 we can access:ob2.a;ob2.b;ob2.show();ob2.c;ob2.d;ob2.print();

B ob2=new B( );A ob1=ob2; //child class reference assigned to parent class

By using ob1 we can access:ob1.a;ob1.b;ob1.show();

By using ob2 we can access:ob2.a;ob2.b;ob2.show();ob2.c;ob2.d;ob2.print();

When child class has method with same signature(i.e. same name and same arguments) asin parent class then parent class method is overridden. For example:

class A{

int a=5;int b=6;void show( ){ System.out.println(“Show in A”); }

}

class B extends A{

int c=10;int d=20;void show(){ System.out.println(“Show in B”); }

}

A ob1=new A( );B ob2=new B( );

By using ob1 we can access:ob1.a;ob1.b;ob1.show(); //Show in A

By using ob2 we can access:ob2.a;ob2.b;ob2.show(); //Show in Bob2.c;ob2.d;

B ob2=new B( );A ob1=ob2; //child class reference assigned to parent class

By using ob1 we can access:ob1.a;ob1.b;ob1.show(); //Show in B

By using ob2 we can access:ob2.a;ob2.b;ob2.show(); //Show in Bob2.c;ob2.d;

For more Visit:http://gsb-programming.blogspot.in/search/label/Java