جلسه ۲۹: کلید واژه super در جاوا

class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
//super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
اکنون اگر خط کامنت شده فوق را در کد از حالت کامنت خارج کنیم و دوباره سعی کنیم کد را اجرا کنیم می بینیم که این بار بدون خطا برنامه اجرا می شود.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
توجه: در یک سازنده می توانیم یک فراخوانی به()super
یا()this
داشته باشیم اما نه هر دو. همچنین ، این نوع فراخوانی ها فقط در داخل سازنده ها قابل استفاده هستند.
این توضیحات در مورد کلید واژه super
تقریباً کافی است. در جلسه بعدی ، در مورد انواع مختلف وراثت بحث خواهیم کرد.
super(); //calls the (no argument) constructor if a no-argument constructor is defined in the SuperClass
super(parameters); //calls the parameterized constructor of the SuperClass with matching parameters from the SubClass constructor
دو خط فوق ، نحو عمومی برای فراخوانی سازنده والد است.
توجه: فراخوانی سازنده والد با استفاده از()super
معمولاً اولین خط کد در داخل سازنده فرزند است. اگر()super
را در سازنده فرزند فراخوانی نکنیم ، سازنده پیش فرض بدون آرگمان والد به طور خودکار فراخوانی می شود. اگر بخواهیم سازنده پارامتری والد را فراخوانی کنیم الزاماً باید به صورت صریح از فراخوانیsuper(parameters)
استفاده شود.
بیایید به نمونه ای از فراخوانی سازنده با استفاده از ()super نگاه کنیم. توجه: کد زیر خطایی ایجاد می کند زیرا از داخل سازنده فرزند هیچ فراخوانی به سازنده والد وجود ندارد.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
//super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
اکنون اگر خط کامنت شده فوق را در کد از حالت کامنت خارج کنیم و دوباره سعی کنیم کد را اجرا کنیم می بینیم که این بار بدون خطا برنامه اجرا می شود.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
توجه: در یک سازنده می توانیم یک فراخوانی به()super
یا()this
داشته باشیم اما نه هر دو. همچنین ، این نوع فراخوانی ها فقط در داخل سازنده ها قابل استفاده هستند.
این توضیحات در مورد کلید واژه super
تقریباً کافی است. در جلسه بعدی ، در مورد انواع مختلف وراثت بحث خواهیم کرد.
class Vehicle { //Base class vehicle
public void display() { //display method inside SuperClass
System.out.println("I am from the Vehicle Class");
}
}
class Car extends Vehicle { // sub class Car extending from Vehicle
public void display() { //display method inside SubClass
System.out.println("I am from the Car Class");
}
public void printOut(){
System.out.println("The display() call with super:");
super.display(); //calling the display() of Vehicle(SuperClass)
System.out.println("The display() call without super:");
display(); //calling the display() of the Car(SubClass)
}
}
class Main {
public static void main(String[] args) {
Car corolla = new Car();
corolla.printOut();
}
}
استفاده در سازنده
یکی دیگر از موارد بسیار مهم استفاده از کلمه کلیدی super
، فراخوانی سازنده والد از داخل سازنده فرزند است.
نکته مهم: چون فرزند از جنس والد نیز هست ، وقتی یک شی از نوع فرزند را ایجاد می کنید ، به صورت ضمنی سازنده والد نیز فراخوانی می شود. زیرا سازنده والد باید فیلد های والد را مقدار دهی کند.
نحوه فراخوانی سازنده والد به صورت زیر است:
super(); //calls the (no argument) constructor if a no-argument constructor is defined in the SuperClass
super(parameters); //calls the parameterized constructor of the SuperClass with matching parameters from the SubClass constructor
دو خط فوق ، نحو عمومی برای فراخوانی سازنده والد است.
توجه: فراخوانی سازنده والد با استفاده از()super
معمولاً اولین خط کد در داخل سازنده فرزند است. اگر()super
را در سازنده فرزند فراخوانی نکنیم ، سازنده پیش فرض بدون آرگمان والد به طور خودکار فراخوانی می شود. اگر بخواهیم سازنده پارامتری والد را فراخوانی کنیم الزاماً باید به صورت صریح از فراخوانیsuper(parameters)
استفاده شود.
بیایید به نمونه ای از فراخوانی سازنده با استفاده از ()super نگاه کنیم. توجه: کد زیر خطایی ایجاد می کند زیرا از داخل سازنده فرزند هیچ فراخوانی به سازنده والد وجود ندارد.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
//super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
اکنون اگر خط کامنت شده فوق را در کد از حالت کامنت خارج کنیم و دوباره سعی کنیم کد را اجرا کنیم می بینیم که این بار بدون خطا برنامه اجرا می شود.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
توجه: در یک سازنده می توانیم یک فراخوانی به()super
یا()this
داشته باشیم اما نه هر دو. همچنین ، این نوع فراخوانی ها فقط در داخل سازنده ها قابل استفاده هستند.
این توضیحات در مورد کلید واژه super
تقریباً کافی است. در جلسه بعدی ، در مورد انواع مختلف وراثت بحث خواهیم کرد.
class Vehicle { //Base class vehicle
int fuelCap = 90; //fuelCap field inside SuperClass
}
class Car extends Vehicle { // sub class Car extending from Vehicle
int fuelCap = 50; //fuelCap field inside SubClass
public void display() {
//accessing the field of parent class using super*/
System.out.println("Fuel Capacity from the Vehicle class: " + super.fuelCap);
//without using super the field of current class shadows the field of parant class*/
System.out.println("Fuel Capacity from the Car class: " + fuelCap);
}
}
class Main {
public static void main(String[] args) {
Car corolla = new Car();
corolla.display();
}
}
فراخوانی متدی از کلاس والد
درست مانند فیلدها ، از کلید واژه super
برای یافتن متدهای والد نیز استفاده می شود. هر زمان که کلاس والد و کلاس فرزند دارای متدی همنام باشند ، از کلمه super
در درون کلاس فرزند برای دسترسی به این متد از والد استفاده می کنیم. بیایید مثالی بزنیم:
class Vehicle { //Base class vehicle
public void display() { //display method inside SuperClass
System.out.println("I am from the Vehicle Class");
}
}
class Car extends Vehicle { // sub class Car extending from Vehicle
public void display() { //display method inside SubClass
System.out.println("I am from the Car Class");
}
public void printOut(){
System.out.println("The display() call with super:");
super.display(); //calling the display() of Vehicle(SuperClass)
System.out.println("The display() call without super:");
display(); //calling the display() of the Car(SubClass)
}
}
class Main {
public static void main(String[] args) {
Car corolla = new Car();
corolla.printOut();
}
}
استفاده در سازنده
یکی دیگر از موارد بسیار مهم استفاده از کلمه کلیدی super
، فراخوانی سازنده والد از داخل سازنده فرزند است.
نکته مهم: چون فرزند از جنس والد نیز هست ، وقتی یک شی از نوع فرزند را ایجاد می کنید ، به صورت ضمنی سازنده والد نیز فراخوانی می شود. زیرا سازنده والد باید فیلد های والد را مقدار دهی کند.
نحوه فراخوانی سازنده والد به صورت زیر است:
super(); //calls the (no argument) constructor if a no-argument constructor is defined in the SuperClass
super(parameters); //calls the parameterized constructor of the SuperClass with matching parameters from the SubClass constructor
دو خط فوق ، نحو عمومی برای فراخوانی سازنده والد است.
توجه: فراخوانی سازنده والد با استفاده از()super
معمولاً اولین خط کد در داخل سازنده فرزند است. اگر()super
را در سازنده فرزند فراخوانی نکنیم ، سازنده پیش فرض بدون آرگمان والد به طور خودکار فراخوانی می شود. اگر بخواهیم سازنده پارامتری والد را فراخوانی کنیم الزاماً باید به صورت صریح از فراخوانیsuper(parameters)
استفاده شود.
بیایید به نمونه ای از فراخوانی سازنده با استفاده از ()super نگاه کنیم. توجه: کد زیر خطایی ایجاد می کند زیرا از داخل سازنده فرزند هیچ فراخوانی به سازنده والد وجود ندارد.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
//super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
اکنون اگر خط کامنت شده فوق را در کد از حالت کامنت خارج کنیم و دوباره سعی کنیم کد را اجرا کنیم می بینیم که این بار بدون خطا برنامه اجرا می شود.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
توجه: در یک سازنده می توانیم یک فراخوانی به()super
یا()this
داشته باشیم اما نه هر دو. همچنین ، این نوع فراخوانی ها فقط در داخل سازنده ها قابل استفاده هستند.
این توضیحات در مورد کلید واژه super
تقریباً کافی است. در جلسه بعدی ، در مورد انواع مختلف وراثت بحث خواهیم کرد.
در این جلسه، با کاربردهای کلمه کلیدی super
در جاوا آشنا خواهید شد. موارد زیر را بیان خواهیم کرد
- کلید واژه
super
چیست؟ - کاربردهای کلید واژه
super
- دسترسی به فیلدهای کلاس والد
- فراخوانی متدی از کلاس والد
- استفاده در سازنده
- کلید واژه
کلید واژه super
چیست؟
همانطور که قبلاً گفته شد کلمه کلیدی this
در جاوا برای اشاره به نمونه (شی) فعلی از کلاس استفاده می شود. به روشی مشابه ، از کلید واژه super
در جاوا برای اشاره به اعضای SuperClass
از درون کلاس فرزند استفاده می شود. کلمه super
فقط در صورت استفاده از وراثت کاربرد دارد.
کاربردهای کلید واژه super
کلید واژه super
در سه زمینه اصلی استفاده می شود:
دسترسی به فیلدهای کلاس والد
فیلد fuelCap
که برای تعیین ظرفیت سوخت یک وسیله نقلیه در داخل کلاس Vehicle
تعریف شده است را در نظر بگیرید. کلاس دیگری به نام Car
از این کلاس Vehicle
مشتق می شود. ما فیلدی را در کلاس Car
با همان نام fuelCap
اما با مقداری متفاوت اعلان می کنیم. حال اگر درون کلاس فرزند (Car) بخواهیم به فیلد fuelCap
از کلاس والد (Vehicle) مراجعه کنیم ، باید از کلمه کلیدی super
استفاده کنیم. این موضوع را در کد زیر می توان دید:
class Vehicle { //Base class vehicle
int fuelCap = 90; //fuelCap field inside SuperClass
}
class Car extends Vehicle { // sub class Car extending from Vehicle
int fuelCap = 50; //fuelCap field inside SubClass
public void display() {
//accessing the field of parent class using super*/
System.out.println("Fuel Capacity from the Vehicle class: " + super.fuelCap);
//without using super the field of current class shadows the field of parant class*/
System.out.println("Fuel Capacity from the Car class: " + fuelCap);
}
}
class Main {
public static void main(String[] args) {
Car corolla = new Car();
corolla.display();
}
}
فراخوانی متدی از کلاس والد
درست مانند فیلدها ، از کلید واژه super
برای یافتن متدهای والد نیز استفاده می شود. هر زمان که کلاس والد و کلاس فرزند دارای متدی همنام باشند ، از کلمه super
در درون کلاس فرزند برای دسترسی به این متد از والد استفاده می کنیم. بیایید مثالی بزنیم:
class Vehicle { //Base class vehicle
public void display() { //display method inside SuperClass
System.out.println("I am from the Vehicle Class");
}
}
class Car extends Vehicle { // sub class Car extending from Vehicle
public void display() { //display method inside SubClass
System.out.println("I am from the Car Class");
}
public void printOut(){
System.out.println("The display() call with super:");
super.display(); //calling the display() of Vehicle(SuperClass)
System.out.println("The display() call without super:");
display(); //calling the display() of the Car(SubClass)
}
}
class Main {
public static void main(String[] args) {
Car corolla = new Car();
corolla.printOut();
}
}
استفاده در سازنده
یکی دیگر از موارد بسیار مهم استفاده از کلمه کلیدی super
، فراخوانی سازنده والد از داخل سازنده فرزند است.
نکته مهم: چون فرزند از جنس والد نیز هست ، وقتی یک شی از نوع فرزند را ایجاد می کنید ، به صورت ضمنی سازنده والد نیز فراخوانی می شود. زیرا سازنده والد باید فیلد های والد را مقدار دهی کند.
نحوه فراخوانی سازنده والد به صورت زیر است:
super(); //calls the (no argument) constructor if a no-argument constructor is defined in the SuperClass
super(parameters); //calls the parameterized constructor of the SuperClass with matching parameters from the SubClass constructor
دو خط فوق ، نحو عمومی برای فراخوانی سازنده والد است.
توجه: فراخوانی سازنده والد با استفاده از()super
معمولاً اولین خط کد در داخل سازنده فرزند است. اگر()super
را در سازنده فرزند فراخوانی نکنیم ، سازنده پیش فرض بدون آرگمان والد به طور خودکار فراخوانی می شود. اگر بخواهیم سازنده پارامتری والد را فراخوانی کنیم الزاماً باید به صورت صریح از فراخوانیsuper(parameters)
استفاده شود.
بیایید به نمونه ای از فراخوانی سازنده با استفاده از ()super نگاه کنیم. توجه: کد زیر خطایی ایجاد می کند زیرا از داخل سازنده فرزند هیچ فراخوانی به سازنده والد وجود ندارد.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
//super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
اکنون اگر خط کامنت شده فوق را در کد از حالت کامنت خارج کنیم و دوباره سعی کنیم کد را اجرا کنیم می بینیم که این بار بدون خطا برنامه اجرا می شود.
class Vehicle { //base class of vehicle
private String make; //
private String color; // Vehicle Fields
private int year; //
private String model; //
public Vehicle(String make, String color, int year, String model) {
this.make = make; //
this.color = color; // Constructor of Vehicle
this.year = year; //
this.model = model; //
}
public void printDetails() { //public method to print details
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
class Car extends Vehicle { //derived class of Car
private String bodyStyle; //Car field
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
public void carDetails() { //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
}
}
class Main {
public static void main(String[] args) {
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
}
}
توجه: در یک سازنده می توانیم یک فراخوانی به()super
یا()this
داشته باشیم اما نه هر دو. همچنین ، این نوع فراخوانی ها فقط در داخل سازنده ها قابل استفاده هستند.
این توضیحات در مورد کلید واژه super
تقریباً کافی است. در جلسه بعدی ، در مورد انواع مختلف وراثت بحث خواهیم کرد.