class Phone {
String model;
String color;
int price;
}
public class Main {
public static void main(String[] args) {
Phone galaxy = new Phone();
galaxy.model = "Galaxy10";
galaxy.color = "Black";
galaxy.price = 100;
Phone iphone =new Phone();
iphone.model = "iPhoneX";
iphone.color = "Black";
iphone.price = 200;
System.out.println("I bought " + galaxy.model + galaxy.color + " + this color " + galaxy.price + ".");
System.out.println("My frined bought " + iphone.model + iphone.color + " + color" + iphone.price + ".");
}
}
A class called Phone contains information about the model, color, and price of the computer. Using this, I created different instances of galaxy and iphone with the same properties such as model, color, and price.
let's talk about methods. You can think of a method as a bundle of code that performs a certain task.
Why do you need a method?
```
int[] heights = new int[5]; // array of keys
initHeight(heights); // 1. Initialize the key
sortHeight(heights); // 2. Sort the keys in ascending order
printHeight(heights); // 3. Print the sorted keys
```
The method name is the name, and the code to be executed is the execution code, but what is the return type? The method returns the result of execution through the return statement. In this case, the part that determines the data type of the result is the return type.
For example, mammals, birds, and reptiles inherit the characteristic 'moving' of animals.
In other words. This means that the method move() can be used by mammals, birds, and reptiles.
As another example, if a variable is also declared in an object, it will be inherited.
If the age variable is declared in the animal object, it is inherited.
The form of inheritance differs depending on whether the method OR variable is 'implemented' or 'used' as it is.
In fact, extends is a representative form of inheritance.
You can use the parent's method as it is, and you can directly use the one implemented in the parent without overriding it.
class Vehicle {
protected int speed = 3;
public int getSpeed(){
return speed;
}
public void setSpeed(int speed){
this.speed = speed;
}
}
class Car extends Vehicle{
public void printspd(){
System.out.println(speed);
}
}
public class ExtendsSample {
public static main (String[] args){
Car A = new Car();
System.out.println(A.getSpeed());
A.printspd();
}
}
A class called Car inherits from Vehicle. And I printed speed with getSpeed() method.
However, I used speed directly in the vehicle's getSpeed method. Extends means that not only methods but also variables can be used.
Multiple inheritance means that more than one parent class can exist, but Java does not support this. That is, public class Son extends Father, Mother{...} This is not supported.
interface TestInterface{
public static int num = 8;
public void fun1();
public void fun2();
}
class InterfaceExam implements TestInterface{
@Override
public void fun1(){
System.out.println(num);
}
@Override
public void fun2() {
}
}
public class InterfaceSample{
public static void main(String args[]){
InterfaceExam exam = new InterfaceExam();
exam.fun1();
}
}
The biggest feature of implements is that the parent method must be overridden (overridden) like this.
Also, this implements replaces multiple inheritance.
extends extends a class and implements implements an interface.
The difference between an interface and an ordinary class is that the interface does not need to implement the defined method.
You just need to implement the method defined in the interface in the class that inherits the interface.