Posts

Showing posts from 2013

Multilevel Inheritance in java with public variables and parameterized Constructor

This is a demonstration of Multi-level inheritance in java using parameterised constructor
package com.inheritance;

public class Class1 {
public int a;
public int b;
public Class1(int a, int b) {
super();
this.a = a;
this.b = b;
System.out.println("I am super class called first--->"+(a+b));
System.out.println(a+b);
}
}
--------------------------------------------- package com.inheritance; public class Class2 extends Class1 { public int c; public Class2(int a, int b, int c) { super(a, b); this.c=c; System.out.println("I am the second level subclass--->" +a%c); }
} -------------------------------------------------------------- package com.inheritance;
public class Class3 extends Class2 { public int d; public Class3(int a, int b,int c, int d) { super(a, b, c); this.d=d; System.out.println("I am thrid level subclass extends second level class-->"+a*b*c*d); // TODO Auto-generated constructor stub } public static void main(String args[]){ Class3 class3=new Class3(1, 2, 3, 4…

Can a Class implement two interfaces with the same method signature

I created two interfaces with same method signature.
You can actually implement the method only once.

package com.anjana;
public interface Interface2 {
public String printme();
}
package com.anjana;
public interface Interface1 {
public String printme();
}
package com.anjana;

public class Employee implements Interface1,Interface2,Interface3{
@Override
public String printme() {
return "Hello";
}

public static void main(String args[]){
Interface1 emp1=new Employee();
emp1.printme();
System.out.println(emp1.printme());
Interface2 emp2=new Employee();
System.out.println(emp2.printme());
}
This will be misleading if you have to do some operations based on the returned value.

Example:Immutable Class

Immutable class is thread safe
Imuutable class objects cannot be modified
Examples :
IntegerString package com.demo.immutable;
//make ur class final
public final class MyImmutable {
 //make ur instance variables final
    final String s="Anjana";
    private String s2=null;
//access the private via constructors--to modify
    public MyImmutable(String s2) {
        super();
        this.s2 = s2;
    }
    public static void main(String args[]){
        MyImmutable  myimmutable=new MyImmutable("dff");
    }
}