Inheritance in Java

 package com.company;

class Base{

    int x;


    public void setX(int x) {

        System.out.println("I am in base setting X now...");

        this.x = x;

    }


    public int getX() {

        return x;

    }


    public void printMe(){

        System.out.println("I am a constructor");

    }

}


class Derived extends Base{

    int y;


    public int getY() {

        return y;

    }


    public void setY(int y) {

        this.y = y;

    }

}



public class tuts {

    public static void main(String[] args) {

        Base b = new Base();

        b.setX(3);

        System.out.println(b.getX());


        Derived d = new Derived();

        d.setX(44);

        System.out.println(d.getX());

    }

}


Comments

Popular posts from this blog

Creating a Thread by Extending Thread class

Constructors from Thread class in Java