Points to note while declaring vars in interface:
1) Variables declared in an interface are public static and final and are initialized before use
Ex:
public interface Bag {
int i=90; //Fine and i is static final variable
//private int k=90; Gives u an error as the variable cant be private.
public String simpleBag();
}
2) Which means u can just references these vars in ur impl classes but u cannot assign them a new value
Abstract Class:
1) You can define private variables in abstract class. They juts behave as variables declared in any other class.
Some code Examples:
public abstract class Badge {
int badgeId=100;
public int getBadgeId() {
return badgeId;
}
public void setBadgeId(int badgeId) {
this.badgeId = badgeId;
}
}
Implementation Class:
public class JPMCBadge extends Badge {
int badgeId=200;
public void showBadge(){
System.out.println(this.badgeId);
//this.badgeId=200;
}
public void newMethodInJpmcBadge(){
System.out.println("This is a new Method in JPMC Badge");
}
}
Main Class:
public class BadgeMain {
public static void main(String[] args) {
Badge badge=new JPMCBadge();
System.out.println(badge.badgeId); //This would print out 100 as we are still referring to badge
JPMCBadge badge2=new JPMCBadge();
badge2.newMethodInJpmcBadge();
System.out.println(badge2.badgeId); //this prints 200
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment