inheritance in java
Java
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
/* output:
Tuut, tuut!
Ford Mustang
*/
public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
Also in Java:
- Title
- how to skip a line in java
- Category
- Java
- Title
- how to use the this keyword in java
- Category
- Java
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- how to get the screen dimensions in java
- Category
- Java
- Title
- nikita kaksharov
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- java read file bufferedreader
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- how to use beacon power in minecraft in java edition
- Category
- Java
- Title
- replace all punctuation in string java
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- set color of text for jlabel
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- object orientation in java
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- How to print in console java
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- run spring boot application command line
- Category
- Java
- Title
- partial view tiles spring
- Category
- Java
- Title
- check if a string is a substring of another string java
- Category
- Java
- Title
- para que sirve getcontentpane en java
- Category
- Java
- Title
- how timer class and display to jtextfield w3schools in java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- java code to get all leaf nodes of a xml file
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- remove spaces at beginning and end of string java
- Category
- Java
- Title
- priority queue in java
- Category
- Java
- Title
- java how to make a string lowercase
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- java return new instance of generic type
- Category
- Java
- Title
- bukkit register commands
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java swing change label icon
- Category
- Java
- Title
- editer un label java
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- get host from request object java
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- The specified child already has a parent. You must call removeView() on the child's parent first
- Category
- Java
- Title
- how to check type of primitive value in java
- Category
- Java
- Title
- java get environment variables
- Category
- Java
- Title
- java display message
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- java split into arraylist
- Category
- Java
- Title
- in java how to convert string to integer
- Category
- Java
- Title
- java bucket sort
- Category
- Java
- Title
- removeeventlistener
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- how to take input in java
- Category
- Java
- Title
- java mockito any string import
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- how-to-use-volley-string-request-in-android
- Category
- Java
- Title
- java constructor genertic
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- java loop through arraylist
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- java insert element at position
- Category
- Java
- Title
- java write arraylist of objects to file
- Category
- Java
- Title
- new hashmap java
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- import scanner
- Category
- Java
- Title
- Java arraylist if you don't want to use add()
- Category
- Java
- Title
- reentrantreadwritelock java
- Category
- Java
- Title
- android java how to stop users fromgoing back too much
- Category
- Java
- Title
- How do you count characters in a string array in Java?
- Category
- Java
- Title
- how to print to the console in java
- Category
- Java
- Title
- get raondom from array java
- Category
- Java
- Title
- java resultset to table
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- java classes and methods
- Category
- Java
- Title
- swapping of two numbers in java
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- log4j with spring boot restful services
- Category
- Java
- Title
- count the number of words in a string java
- Category
- Java
- Title
- iterate map in java 8 using stream
- Category
- Java
- Title
- append to arraylist by index java
- Category
- Java
- Title
- java break in foreach
- Category
- Java
- Title
- com.android.builder.dexing.DexArchiveMergerException:
- Category
- Java
- Title
- changing double space to single in java script
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- get first 5 characters of string java
- Category
- Java
- Title
- how to add two map values in java
- Category
- Java
- Title
- add two numbers bitwise
- Category
- Java
- Title
- how to create a thread local variable in java
- Category
- Java
- Title
- how to test for legit email in java
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- java prev_permutation
- Category
- Java
- Title
- if else program in java
- Category
- Java
- Title
- Don't use a line-beased input after a token-based input.
- Category
- Java
- Title
- java code to get all leaf nodes of a xml
- Category
- Java