This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2021/11/23

Abstract and super keyword

Roshan123 Roshan123

2021/11/23

#
1) When m i supposed to use super keyword and how to use? ( I want the explanation of .... super.Adfde ) By the way is super(222,234,1, true); a modified keyword in greenfoot? 2) same goes for abstract keyword....when and how I have already seen some explanation but i need some more 3) int, char boolean are known as primitive data type likewise what are class, abstract and interface called as?
danpost danpost

2021/11/23

#
Roshan123 Roshan123

2021/11/23

#
Plz check my answer (1)
public class danpost
{
  boolean inDan=true;
}
public class Rr extends danpost
{
   public void printBoolean()
   { System.out.print("Your answer is: "+super.inDan); }
}
public class plzPrintIt
{
   public static void main()
    {
       Rr rr = new Rr();
       rr.printBoolean();
    }
}
Output: Your answer is: true super keyword can be used without creating an instance of its parent class and can call each and every constructors, method from it parent class (2)
abstract class RcCookie  
{  
    abstract void TastyCookie();        
}  
class Rr extends RcCookie  
{   
   void TastyCookie() 
   {  
    System.out.printlnn("danpost");       
   }  
}  
  
public class cheese
{  
  
    public static void main()
    {  
  
    Rr rr=new Rr();  
    rr.TastyCookie();  
    }         
}
Output: danpost If i define any method as abstract then no matter what I can call the method from parent class and then implement it in its subclass without any initialisation of its Parent class Is it CorrAct!?
danpost danpost

2021/11/23

#
(1) looks good; (2) syntax error on line 9 -- should be "println", not "printlnn";
without any initialisation of its Parent class
When an object is created from the child class, the parent classes are all initialised with it. The parent classes are all a part of (inherited into) the child class. As well, the object can be referenced as any of its parents' type, plus its own class' type. However, to access a method or field belonging to that object (whether inherited or not), it must be referenced as a Type at least down to where the method or field is declared.
Object world = new MyWorld();
int ww = world.getWidth(); // fails
// Object class, which has no parents, does not declare a getWidth method

World world = new MyWorld();
int ww = world.getWidth(); // succeeds
// method is declared in the World class

Object world = new MyWorld();
int ww = ((World)world).getWidth(); // succeeds
// Object is typecast to World where method is declared

MyWorld world = new MyWorld();
int ww = world.getWidth(); // succeeds
// method is inherited into MyWorld class from World
Roshan123 Roshan123

2021/11/24

#
danpost wrote...
(2) syntax error on line 9 -- should be "println", not "printlnn";
Ohh sorry. But yet now i m not clear....why the heck we use abstract method ? The thing which i understood is that we use abstract class to implement the same method in every child class i.e. every human has health so its a abstract method as health to all human being. Though yet now i have not seen an perfect example of usage of abstract method so can u please tell me in what situation will i use abstract method?...i don't know whether the human example which i gave is perfect/correct example of an abstract method
danpost danpost

2021/11/24

#
Roshan123 wrote...
now i m not clear....why the heck we use abstract method ?
Abstract methods can be provided in abstract classes to "force" its subclasses to implement (override, in a sense) those methods. A good example is of their use is given here.
The thing which i understood is that we use abstract class to implement the same method in every child class i.e. every human has health so its a abstract method as health to all human being.
"health" is a state, which fields are used for. Methods provide behavior; so, maybe adjustHealth would be appropriate as a behavior.
Though yet now i have not seen an perfect example of usage of abstract method so can u please tell me in what situation will i use abstract method?...i don't know whether the human example which i gave is perfect/correct example of an abstract method
The example from the link above should be sufficient.
danpost danpost

2021/11/24

#
As another example, my Dialog class has an abstract Element class which is subclassed by all the GUI objects (Checkbox, Button, TextBox, ListBox, etc). It has a bunch of fields that each sub-type would use, plus one abstract method which requires the extending classes to implement an updateImage method. This allows other methods provided in the Element class to call the implementation when a field value is changed -- a dimension (width or height), a color (background, text or frame), a style (font size or frame thickness), shown text, etc., almost all fields, if not all, that are provided in the Element class. The Button class was also made abstract because all buttons need to be implemented to do something when clicked on; so, an abstract method called onClick was used there.
Roshan123 Roshan123

2021/11/25

#
danpost wrote...
A good example is of their use is given here.
Got it!!! Very helpful! One more last questions.... (1) whenever i create a subclass of World then the subclass already contains some comments, method, a keyword, namely "super(int, int, int, boolean)" and a int variable (i think so)..... SonPlz tell me how will i automatically add this all stuff automatically whenever i create a child class?
danpost danpost

2021/11/25

#
Roshan123 wrote...
whenever i create a subclass of World then the subclass already contains some comments, method, a keyword, namely "super(int, int, int, boolean)" and a int variable (i think so)..... SonPlz tell me how will i automatically add this all stuff automatically whenever i create a child class?
If you want to modify what initially appears when you create a class, edit the actorJava and worldJava files in directory: /greenfoot/lib/english/greenfoot/templates/
Roshan123 Roshan123

2021/11/27

#
danpost wrote...
If you want to modify what initially appears when you create a class, edit the actorJava and worldJava files in directory: /greenfoot/lib/english/greenfoot/templates/
Very good! Finally u have learnt it! I m sorry....ignore the above message I m always thankful to u for helping me everytime
You need to login to post a reply.