Skip to content

Instantly share code, notes, and snippets.

@arttuladhar
Created May 14, 2013 20:35
Show Gist options
  • Save arttuladhar/5579300 to your computer and use it in GitHub Desktop.
Save arttuladhar/5579300 to your computer and use it in GitHub Desktop.
Extending Classes - Java Tuts
package com.aayushtuladhar.HelloWorld;
class B {
protected int x;
int getX() { return x; }
void setX(int x) { this.x = x; }
void increase() { x = x+1; }
void decrease() { x = x-1; }
int returnx() { return x; }
}
class C extends B {
void increase() { x = x+10; }
void decrease() { x = x-10; }
}
public class Attr {
public static void main(String[] args) {
B bClass = new B();
bClass.setX(3);
bClass.increase();
System.out.println(bClass.returnx());
C cClass = new C();
cClass.setX(3);
cClass.increase();
System.out.println(cClass.returnx());
B testClass = new C();
testClass.setX(5);
testClass.increase();
System.out.println(testClass.returnx());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment