Inheritance, Interfaces, Polymorphism, Method Overriding
When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write. This is known 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, extended class, or child class). The class from which the subclass is derived is called a superclass (also a 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 its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. to refer to superclass we use the keyword: super class 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: 10 a 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 to super 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; } Now c obj=new c( ); System.out.println(“A: “+a); System.out.println(“B: “+b); System.out.println(“C: “+c); will output: A: 5 B: 10 C: 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; @override public void show() //Must write public { System.out.println(“A of interface: ”+MyInterface.a); //a is static so refer it using interface name System.out.println(“A of class: ”+a); } }
class Demo extends AnotherClass implements MyInterface { int a=10; @override public 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) as in 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 B ob2.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 B ob2.c; ob2.d;
For more Visit: http://gsb-programming.blogspot.in/search/label/Java

Learn Java Part 11

  • 1.
  • 2.
    When you wantto create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write. This is known 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; } }
  • 3.
    A class thatis derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a 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 its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. to refer to superclass we use the keyword: super class 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: 10 a of super class: 5
  • 4.
    Same class Otherclass 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
  • 5.
    class a { int a=5; a(intvalue) { 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 to super class constructor. Using: super(values);
  • 6.
    class a { private inta=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 } }
  • 7.
    class a { int a=5; } classb extends a { int b=10; } class c extends b { int c=20; } Now c obj=new c( ); System.out.println(“A: “+a); System.out.println(“B: “+b); System.out.println(“C: “+c); will output: A: 5 B: 10 C: 20
  • 8.
    • All variablesof 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
  • 9.
    class Demo implementsMyInterface { int a=10; @override public void show() //Must write public { System.out.println(“A of interface: ”+MyInterface.a); //a is static so refer it using interface name System.out.println(“A of class: ”+a); } }
  • 10.
    class Demo extendsAnotherClass implements MyInterface { int a=10; @override public void show() { //statements } }
  • 11.
    Child class referencecan 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); } }
  • 12.
    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();
  • 13.
    When child classhas method with same signature(i.e. same name and same arguments) as in 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”); } }
  • 14.
    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 B ob2.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 B ob2.c; ob2.d;
  • 15.