int a = 10;
a=5; //a값이 5로 바뀔 수 있다.
final int b = 10;
b = 10; //이것은 오류가 남. 정해진 특정 값 이외에 다른 값을 넣으면 안 될 때 오류를 발생시켜준다.
사자는 동물입니다. (o)
동물은 사자입니다. (x)
//final 클래스 변수명 앞에 올 경우
class Person {
String name = "홍길동";
int age = 20;
final static String ADDR = "서울시 마포구 서교동"; //final이면 static이다. (상수는 모두 대문자로 표현하여 일반 멤버변수와 구분한다.)
//final은 어차피 못 바꾸는 것이라서 객체마다 따로 메모리를 잡을 필요가 없다.
//그래서 final이면 static으로 만들어서 모든 객체가 같이 사용한다.
}
public class FinalTest01 {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
p1.age = 21;
p2.age = 31; //값이 변할 수 있음.
//p1.ADDR = "제주도 서귀포시 애월읍"; //final로 값을 주었기 때문에 대입해서 값을 바꿀 수 없음. 오류가 발생
//p2.ADDR = "경기도 파주시" //addr은 상수이기 때문에 값을 변경할 수 없다.
}
}
//final 클래스의 메소드 앞에 올 경우
class A {
public void pro() {
System.out.println("A의 pro 입니다.");
}
public final void hello() { //자식이 오버라이딩 하지 못하게 final 사용
System.out.println("hello 입니다.");
}
}
class B extends A {
public void pro() {
System.out.println("B의 pro입니다.");//오버라이딩함.(자신에게 맞게끔)
}
// public void hello() { //부모로부터 물려받은 final메소드는 오버라이딩 할 수 없다.
//
// }
}
public class FinalTest02 {
public static void main(String[] args) {
}
}
//final 클래스의 클래스 이름 앞에 올 경우
final class Bird {
String name;
String color;
boolean wing;
public void fly() {
System.out.println("훨~훨~");
}
}
//final 클래스는 상속받을 수 없다.
//class Plane extends Bird {
//
//}
public class FinalTest03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class A {
String title = "hello";
int year = 2022;
}
class B extends A {
String title = "java";
String addr = "서울";
public void info() {
System.out.println("year: " + year);
System.out.println("addr: " + addr);
System.out.println("title: " + title);
System.out.println("부모의 title: " + super.title); //부모의 title을 출력하고 싶을 때 super.title
}
}
public class SuperTest {
public static void main(String[] args) {
B ob = new B();
ob.info();
}
}
class Person {
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
class Student extends Person {
String major;
public Student(String name, int age, String major) {
super(name, age); //부모의 매개변수를 갖는 생성자를 동작시킴.
this.major = major;
}
}
public class SuperTest02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
마트에서 판매되는 물건을 클래스 Product로 나타내자. Product 클래스에서 상속받아서 할인 물건을 나타내는 DiscountProduct 클래스를 작성할 수 있다.
class Product { // 판매되는 물건
protected String name;
protected int price; //물건의 가격
public double getPrice() {
return price;
}
public Product(String name, int price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + "]"; // + ", getName()=" + getName() + "]";
}
}
class DiscountProduct extends Product {
private double discount;
private double discount_price;
public DiscountProduct(String name, int price, double discount) {
super(name, price);
this.discount = discount;
}
@Override
public double getPrice() {
discount_price = price - ((price/100)* discount);
return discount_price;
}
@Override
public String toString() {
return "DiscountProduct [discount=" + discount + ", name=" + name + "]";
}
}
public class ProductTest {
public static void main(String[] args) {
Product p1 = new Product("toothbrush", 3000);
Product p2 = new DiscountProduct("toothbrush", 3000, 15);
System.out.println(p1.getPrice());
System.out.println(p2.getPrice());
System.out.println(p1);
System.out.println(p2);
}
}