Created
May 14, 2013 20:35
-
-
Save arttuladhar/5579300 to your computer and use it in GitHub Desktop.
Extending Classes - Java Tuts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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