inheritance setter and getter in java
Java
public class C2 extends C1{
}public class A {
private int a;
public int getA() {
return a;
}
public void setA(int value) {
a = value;
}
}C1.x = 1
C2.x = 1
C3.x = 1
C4.x = 4public class C3 extends C2{
protected int x = 3;
}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());
}
}public class B extends A {
@Override
public final int getA() {
return super.getA();
}
@Override
public final void setA(int value) {
super.setA(value);
}
}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+"\n"+"Number of wheels:"+numWheels+""
+ "\n"+
"Manufacturer:"+manufacturer+"\n"+
"Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
"\n");
}
}
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+"\n"+
"Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
"\n"+"Seat type:"+seat+"\n");
}
}
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+"\n"+
"Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
"\n");
}
}
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());
}
}public class C4 extends C3{
protected int x = 4;
@Override
public int getX() {
return x;
}
}
Also in Java:
- Title
- how to create a Rectangle in java
- Category
- Java
- Title
- how to remove all special characters from a string in java
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- regex get string between quotes java
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- get last element of array java
- Category
- Java
- Title
- parallel sorting in java 8
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- color from hex code flutter
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- java count substring occurrences in string
- Category
- Java
- Title
- set preference value android
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- java nextpermutation
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- java append to array
- Category
- Java
- Title
- java "->"
- Category
- Java
- Title
- system.arraycopy java
- Category
- Java
- Title
- init cap java
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- enum java
- Category
- Java
- Title
- capcitor FERR_CLEARTEXT_NOT_PERMITTED
- Category
- Java
- Title
- java while loop break
- Category
- Java
- Title
- javafx textarea size
- Category
- Java
- Title
- get cursor position in textarea java
- Category
- Java
- Title
- set iteration java
- Category
- Java
- Title
- how to remove spaces from an array in java
- Category
- Java
- Title
- demo java file
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- java open file
- Category
- Java
- Title
- substring java
- Category
- Java
- Title
- java mouseevent
- Category
- Java
- Title
- java script to detect and launch all browsers
- Category
- Java
- Title
- dictionary in java
- Category
- Java
- Title
- how to do for each in java
- Category
- Java
- Title
- islowercase java
- Category
- Java
- Title
- java script print date in YYYY-MM-DD HH:MM:SS format
- Category
- Java
- Title
- priority queue in java
- Category
- Java
- Title
- java Convert a string IPv4 IP address to the equivalent long numeric value.
- Category
- Java
- Title
- convert string into unicode java
- Category
- Java
- Title
- binary search java
- Category
- Java
- Title
- how to use an abstract class in java
- Category
- Java
- Title
- objectoutputstream exemple
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- java double format
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- read from elasticsearch in spark
- Category
- Java
- Title
- java read file from command line argument
- Category
- Java
- Title
- print int in java
- Category
- Java
- Title
- saving String character in arraylist
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- java stack methods
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- editer un label java
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- Java If statemtn
- Category
- Java
- Title
- convert string to localdatetime
- Category
- Java
- Title
- split method in java
- Category
- Java
- Title
- how to echo java_home in windows cmd
- Category
- Java
- Title
- non primitive data types in java
- Category
- Java
- Title
- how to remove duplicates from an array java
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- remove unused imports intellij
- Category
- Java
- Title
- listview get selected java
- Category
- Java
- Title
- imagesbutton how to set background
- Category
- Java
- Title
- java binary exponentiation
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- error message pushes button down
- Category
- Java
- Title
- java how to print a string[]
- Category
- Java
- Title
- android java how to stop users fromgoing back too much
- Category
- Java
- Title
- integer max value java
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- java stack empty
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- Primitive Type vs. Reference Type
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- how to create a JFrame in java
- Category
- Java
- Title
- how to make a button disapear on click in javafx
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- try block in java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- how to get the screen dimensions in java
- Category
- Java
- Title
- catch array out of bounds exception java
- Category
- Java