জাভায় super কিওয়ার্ড ব্যবহার
java super কিওয়ার্ড ব্যবহার করা হয় parent class এর object .super কিওয়ার্ড ব্যবহার:
- super is used to refer immediate parent class instance variable.
- super() is used to invoke immediate parent class constructor.
- super is used to invoke immediate parent class method.
super is used to refer immediate parent class instance variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// base class Vahicle | |
class Vahicle{ | |
int maxspeed = 300; | |
} | |
//sub class Car extending vahicle | |
class Car extends Vahicle{ | |
int maxspeed = 500; | |
void display(){ | |
// print the max speed of base class | |
System.out.println("Max Speed subclass "+maxspeed); | |
System.out.println("Max Speed"+super.maxspeed); | |
} | |
} | |
// Driver program to Myclass | |
class MyClass{ | |
public static void main(String args[]){ | |
Car obj = new Car(); | |
obj.display(); | |
} | |
} |
super() is used to invoke immediate parent class constructor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* superclass Person */ | |
class Person | |
{ | |
Person() | |
{ | |
System.out.println("Person class Constructor"); | |
} | |
} | |
/* subclass Student extending the Person class */ | |
class Student extends Person | |
{ | |
Student() | |
{ | |
// invoke or call parent class constructor | |
super(); | |
System.out.println("Student class Constructor"); | |
} | |
} | |
/* Driver program to MyClass*/ | |
class MyClass | |
{ | |
public static void main(String[] args) | |
{ | |
Student s = new Student(); | |
} | |
} |
super is used to invoke immediate parent class method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Base class Person */ | |
class Person | |
{ | |
void message() | |
{ | |
System.out.println("This is person class"); | |
} | |
} | |
/* Subclass Student */ | |
class Student extends Person | |
{ | |
void message() | |
{ | |
System.out.println("This is student class"); | |
} | |
// Note that show() is only in Student class | |
void show() | |
{ | |
// will invoke or call current class message() method | |
message(); | |
// will invoke or call parent class message() method | |
super.message(); | |
} | |
} | |
/* Driver program to MyClass */ | |
class MyClass | |
{ | |
public static void main(String args[]) | |
{ | |
Student s = new Student(); | |
// calling show() of Student | |
s.show(); | |
} | |
} |