Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

inheritance setter and getter in java

public class A {
    private int a;
    public int getA() {
        return a;
    }
    public void setA(int value) {
        a = value;
    }
}
Comment

inheritance setter and getter in java

public class C1 {
    protected int x = 1;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public static void main(String[] args){
        System.out.println(new C1().getX());
        System.out.println(new C2().getX());
        System.out.println(new C3().getX());
        System.out.println(new C4().getX());
    }

}
Comment

inheritance setter and getter in java

public class C2 extends C1{
}
Comment

inheritance setter and getter in java

public class C3 extends C2{
    protected int x = 3;
}
Comment

inheritance setter and getter in java

public class C4 extends C3{

    protected int x = 4;

    @Override
    public int getX() {
        return x;
    }

}
Comment

inheritance setter and getter in java

C1.x = 1
C2.x = 1
C3.x = 1
C4.x = 4
Comment

inheritance setter and getter in java

package testvehicle;


public class Car extends Vehicle
{

    private int numDoors;
    private int numWheels;


    public Car(String manufacturer,String model,int maxSpeed,double price,int numWheels
            ,int numDoors)
    {
        super(manufacturer,model,maxSpeed,price);
        this.numDoors=numDoors;
        this.numWheels=numWheels;

    }

    public Car()
    {

    }


    public int getNumDoors()
    {
        return numDoors;
    }


    public void setNumDoors(int numDoors)
    {
        this.numDoors = numDoors;
    }


    public int getNumWheels()
    {
        return numWheels;
    }


    public void setNumWheels(int numWheels)
    {
        this.numWheels = numWheels;
    }

    public String toString()
    {
        return ("Number of doors:"+numDoors+"
"+"Number of wheels:"+numWheels+""
                + "
"+
        "Manufacturer:"+manufacturer+"
"+
               "Model:"+model+"
"+"Maximum Speed:"+maxSpeed+"
"+"Price in euros:"+price+
               "
");
    }

}



package testvehicle;


public class MotorCycle extends Vehicle
{
    private String seat;

    public MotorCycle(String manufacturer,String model,int maxSpeed,double price
            ,String seat)
    {
        super( manufacturer, model, maxSpeed, price);
        this.seat=seat;
    }


    public MotorCycle()
    {

    }


    public String getSeat()
    {
        return seat;
    }


    public void setSeat(String seat)
    {
        this.seat = seat;
    }


    public String toString()
    {
        return ("Manufacturer:"+manufacturer+"
"+
               "Model:"+model+"
"+"Maximum Speed:"+maxSpeed+"
"+"Price in euros:"+price+
               "
"+"Seat type:"+seat+"
");
    }




}



 package testvehicle;

    public abstract class Vehicle//This class doesn't do something!
    {
        protected String manufacturer;
        protected String model;
        protected int maxSpeed;
        protected double price;

        public Vehicle(String manufacturer,String model,int maxSpeed,double price)
        {
            this.manufacturer=manufacturer;
            this.model=model;
            this.maxSpeed=maxSpeed;
            this.price=price;

        }

        public Vehicle()
        {

        }


        public String getManufacturer()
        {
            return manufacturer;
        }


        public void setManufacturer(String manufacturer)
        {
            this.manufacturer = manufacturer;
        }


        public String getModel()
        {
            return model;
        }


        public void setModel(String model)
        {
            this.model = model;
        }


        public int getMaxSpeed()
        {
            return maxSpeed;
        }


        public void setMaxSpeed(int maxSpeed)
        {
            this.maxSpeed = maxSpeed;
        }


        public double getPrice()
        {
            return price;
        }


        public void setPrice(double price)
        {
            this.price = price;
        }



       public String toString()
        {
           return ("Manufacturer:"+manufacturer+"
"+
                   "Model:"+model+"
"+"Maximum Speed:"+maxSpeed+"
"+"Price in euros:"+price+
                   "
");
        }



    }

    package testvehicle;

    public class Main
    {


        public static void main(String[] args)
        {
            Car C=new Car("Opel","Corsa",220,12000.0,4,5);
            MotorCycle M=new MotorCycle("KTM","DUKE-690",250,9000.0,"Agressive");
            System.out.println(C.toString());
            System.out.println();
            System.out.println(M.toString());

        }


    }
Comment

inheritance setter and getter in java

public class B extends A {
    @Override
    public final int getA() {
        return super.getA();
    }
    @Override
    public final void setA(int value) {
        super.setA(value);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java get current free disc space 
Java :: time complexity of indexof java 
Java :: Java schleifen 
Java :: viewresolver in spring boot 
Java :: force_dark_auto android webview 
Java :: leap year java method 
Java :: Android java parse class name via intent 
Java :: pattern exercises for java 
Java :: mergesort parallelization using spark 
Java :: computeifabsent hashmap java 
Java :: Java Standard Library Method 
Java :: how to getobject from id in controlp5 
Java :: dicom read with java 
Java :: print jtable in java 
Java :: byte array to zip java 
Java :: hybrid inheritance in java 
Java :: what is xml file in java 
Java :: BasicAWSCredentials 
Java :: A method that returns a stream of JUnit Arguments 
Java :: Algorithms - count 
Java :: a Java-8 stream of batches, 
Java :: spring generate banner 
Java :: licenceurl 
Java :: opencv copy image java 
Java :: how to get the length of a jagged array java 
Java :: View get text android Close 
Java :: java naming convention acronyms 
Java :: Lists - removing 
Java :: maven show runtime classpath 
Java :: get alpha from image java 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =