Inheritence
Inheritence in Java
package com.company;
class base{ //this is base class
int x;
public int getX() {
return x;
}
public void setX(int x) {
System.out.println("I am in base and setting X now ");
this.x = x;
}
public void PrintMe(){
System.out.println("i am a constructor");
}
}
class Derived extends base{ //this is inherit the properties of base
int y;
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
public class Inheritence {
public static void main(String[] args) {
System.out.println("Derived class");
Derived der =new Derived();
der.setX(5);
System.out.println(der.getX()); //taking values from the base class
der.setY(10);
System.out.println(der.getY());
der.PrintMe();
System.out.println("Base class");
base b=new base();
b.setX(5);
System.out.println( b.getX());
}
}
Comments
Post a Comment