Top Banner
Inheritance, Interfaces, Polymorphism, Method Overriding
15
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Learn Java Part 11

Inheritance, Interfaces, Polymorphism, Method Overriding

Page 2: Learn Java Part 11

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;

}}

Page 3: Learn Java Part 11

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

Page 4: Learn Java Part 11

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

Page 5: Learn Java Part 11

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);

Page 6: Learn Java Part 11

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}

}

Page 7: Learn Java Part 11

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

Page 8: Learn Java Part 11

• 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

Page 9: Learn Java Part 11

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);

}}

Page 10: Learn Java Part 11

class Demo extends AnotherClass implements MyInterface{

int a=10;

@overridepublic void show(){

//statements}

}

Page 11: Learn Java Part 11

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); }

}

Page 12: Learn Java Part 11

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();

Page 13: Learn Java Part 11

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”); }

}

Page 14: Learn Java Part 11

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;

Page 15: Learn Java Part 11

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