answersLogoWhite

0

In object oriented programming, we talk about classes having "super" or "sub" classes, sometimes called "base" and "derived" classes. In all cases this is talking about something called "polymorphism", which basically means the ability to use one object just as you would another. The way this is achieved is by deriving or "subclassing" another class. in C#, you use the : operator as shown here:

class Ford : Car

in Java, you use the extends keyword:

class Ford extends Car

This basically means the "Ford" class will get all of the same features and functions of Car without having to rewrite them -- that is because "Ford" *IS* a "Car".

One example of this is if I have a function that asks for a type of "Car", you can simply pass your "Ford" to it, and it would work without the function even knowing it was dealing with a Sub class.

function string GetName(Car car)

{

return car.getName();

}

Ford f = new Ford();

System.out.println(GetName(f));

If you want to "call" to the super class from within the derived (sub) class -- you can use the identifier "super" (in c#, you use "base"):

protected override getName()

{

return super.getName();

}

Hope that helps!

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

Difference between super and this in java?

The keyword super is used to explicitly call methods/values from the parent class The keyword this is used to explicitly call methods/values from the current class


A subclass can call constructor method defined by its super class?

True


Why we use super in java?

The keyword super is used to refer to the parent class instance of the current class. Lets say we have a class class A extends B { ... public void getName(){ } ... } Lets assume the parent class B also has a method getName() Inside class A if you call getName() it would by default call the current class's method. To make the JVM intentionally call the super class method we can use super if we say super.getName() then the parent class instance of the method would be called.


What you call those subclass having more than one super class?

category


What you call those subclass having more than one super-class?

category


How does constructor work?

First line in any constructor has to be either super() or this() not both. If any constructor does not contain either of super() and this(), compiler adds super(). When any constructor is called before excuting the code of the constructor, if it founds this(), it will call another constructor else it will call super() which is the call for the constructor of super class, now again from the super class constructor it will call the super class constructor if available. This is continued until it reaches the top of the class hierarchy. ---- Basically, a constructor is a block of code that gets executed each time a particular instance of a class is created. So, say you've designed a class for working with a database of some sort. When you create an instance of that class, copies of all the variables and functions of that class get attached to the instance-object, and if one of the functions is a constructor function, it will be run as soon as the instance-object is created. This lets you automatically set up conditions for the instance (i.e. establishing connections to different databases or reading data from different tables, or etc.). Depending on the language you're using, classes may or may not automatically call the constructor function of a parent or super class (if such exists, and if you do not provide a constructor for the class in question).


How super class variable can be used to refer the sub class object?

The super variable is not a reference to a subclass. It is a reference to the superclass. class MyClass { void printType() { System.out.println("This is a MyClass"); } // MySubClass is a subclass of MyClass. Within this class, the super keyword // refers to MyClass. static class MySubClass extends MyClass { void printType() { // Tell Java we also want to call the printType method of the super class super.printType(); System.out.println("This is a MySubClass"); } } }


Brahmin equals Backward Caste that's why Asking for Reservation?

No actually call themselves as super class.


How do you invoke an overwritten super class from the sub class?

super


Explain different use of super with the help of java program?

the keyword super is used to access methods/variables of the parent class. ex: public class A { public String getName(){ ..... } } public class B extends A { public String getName(){ ..... } } If you want to access the method getName of class A inside class B you cannot do directly because the compiler would get confused. you need not create an object of A since it is already available. At this point you can use super super.getName() is enough to call the method of the parent class


What is this and super keyword in java?

"this" represents current instance of a class whereas to access constructor, member variable or method of the super class "super" keyword is used. For ex. public class A { A() {} hello() {}; } public class B { private int x; B() { super(); // call A's constructor } hello(int x) { super.hello(); //calls A's hello() this.x = x; // this.x represents member variable of the class B whereas "x" represents local variable. } }


Difference between super and final keyword?

this is a reference to the current classsuper is a reference to the super class of the current class (the class from which this class extends). You can use super.super if you want to access the second level class. (The class your parent class extends) The purpose of having thisand super keywords is to differentiate between methods and variables in classes that may have the same name as that in the parent class. Under such situations if we want to ensure that only the methods from a particular class only gets called we can use this and super.