I have googled couple of times but still can't understand the supertype method. Can anyone please explain what is this?
-
2you mean methods of super class – Subhrajyoti Majumder Feb 28 '13 at 07:44
-
If I am not wrong then it is the call using `super` to the method of the class which has been extended by another, e.g. `super.onCreate` calls the `onCreate` method of the `Activity class` – Prateek Feb 28 '13 at 07:44
-
could you elaborate ? – TheWhiteRabbit Feb 28 '13 at 07:45
-
Are you clear Object Oriented Programming first? If not I suggest starting with that. – Karthik T Feb 28 '13 at 07:45
-
"Supertype method" is not an official term afaik. Can you give more context? – assylias Feb 28 '13 at 07:46
-
1Supertype is used in JAVA GENERICS. – The Holy Coder Feb 28 '13 at 07:49
6 Answers
There is a notion of supertype and subtype in OOPS, In java this kind of relationship is implemented by inheritance i.e. using extends
keyword:
class A {} // super class
class B extends A {} //sub class
Any member (fields, methods) declared in super class is to be called supertype.
Therefore in above context if class A
has method like
class A {
void set()
}
Set is supertype method for class B
.
However, notice that if there is another class say C
:
class C {
void set()
}
Then set()
method is not supertype for C
class because there is no relationship between class A
and class C
(relationship is created by extends
keyword, for inheritance).
![](../../users/profiles/1133169.webp)
- 1,868
- 3
- 23
- 52
![](../../users/profiles/1207003.webp)
- 3,064
- 5
- 36
- 75
-
Furthermore, an interface is also a supertype of any class that implements it (and of any interface that extends it). That ought to make interface default methods supertype methods as well. – olovb Apr 23 '15 at 23:09
Super at Constructer level
class SuperClass
{
int num=10;
public void display()
{
System.out.println("Superclass display method");
}
}
class SubClass extends SuperClass
{
int num=20;
public void display()
{
System.out.println("the value of subclass variable name:"+num);
System.out.println("Subclass display method");
}
public void Mymethod()
{
super.display();
System.out.println("the value of superclass variable name:"+super.num);
}
public static void main(String[] args)
{
SubClass obj=new SubClass();
obj.Mymethod();
obj.display();
}
}
![](../../users/profiles/7824722.webp)
- 11
- 2
In java every thing are object and a method is also a object of class java.lang.reflect.Method
So the super type of method can be consider as the super class of java.lang.reflect.Method
that is the AccessibleObject
.
![](../../users/profiles/1249225.webp)
- 2,370
- 4
- 25
- 44
if you are talking about calling a super method, you should try the following
create a class with a method public method e.g. printSomething()
public void printSomething() { System.out.println("hello, I am the first class"); }
create a second class which inherites from the first class and override the printSomething method
@override public void printSomething() { super.printSomething(); }
write a small programm which call the method printSomething of class two and see what will happen
![](../../users/profiles/2062634.webp)
- 1,897
- 2
- 20
- 27
Super Type and sub type is a property of inheritance i.e for the purpose of re-usability of code. I am giving you example of Super Class and Sub class. For more you can follow here.
using System;
namespace MultilevelInheritance
{
public class Customer
{
public float fDis { get; set; }
public Customer()
{
Console.WriteLine("I am a normal customer.");
}
public virtual void discount()
{
fDis = 0.3F;
Console.WriteLine("Discount is :{0}", fDis);
}
}
public class SilverCustomer : Customer
{
public SilverCustomer()
: base()
{
Console.WriteLine("I am silver customer.");
}
public override void discount()
{
fDis = 0.4F;
Console.WriteLine("Discount is :{0}", fDis);
}
}
class GoldenCustomer : SilverCustomer
{
public GoldenCustomer()
{
Console.WriteLine("I am Golden customer.");
}
public override void discount()
{
fDis = 0.6F;
Console.WriteLine("Discount is :{0}", fDis);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultilevelInheritance
{
class Program
{
static void Main(string[] args)
{
Customer objCus = new Customer();
objCus.discount();
SilverCustomer objSil = new SilverCustomer();
objSil.discount();
GoldenCustomer objGold = new GoldenCustomer();
objGold.discount();
Console.ReadLine();
}
}
}
-
3Hi, You seem to be adding a link to a website that has ads w/o disclosing affiliation. It looks like you are trying to get more views on the site. See [How to not be a spammer](http://stackoverflow.com/help/promotion). Note that doing so might increase suspicion among the members of the community that may lead to the suspension of your account. Refer this [meta answer](http://meta.stackoverflow.com/a/294124/4099593). I am writing this comment to warn you beforehand. Regards – Bhargav Rao Sep 11 '16 at 15:04
Super is used to invoke parent class Properties used at 3 levels variable constructer and method level
1.Super at Variable
class Super
{
int age;
Super(int age)
{
this.age=age;
}
public void getAge()
{
System.out.println(age);
}
}
class Sub extends Super
{
Sub(int age)
{
super(age);
}
public static void main(String[] args)
{
Super obj=new Super(24);
obj.getAge();
}
}
![](../../users/profiles/5131937.webp)
- 9,483
- 8
- 34
- 74
![](../../users/profiles/7824722.webp)
- 11
- 2