Skip to main content
added 29 characters in body
Source Link
SkyWalker
  • 29.3k
  • 14
  • 77
  • 134

Credit goes to Blackcompe

If you are using the generic version of the list implementation, you don't need to cast, e.g.

ArrayList<BestTutor> list = null; BestTutor c = list.get(0); 

Generics is a type-safety method that tells Java nothing other than a BestTutor will go into this collection, so you can always bet that List.get() will return a BestTutor or whatever the bounded object is. BestTutor is called the bounded object. If you don't use generics the bounded object is Object., e.g.

ArrayList<Object> list; 

Although, this bounding is implicit, so it's just:

ArrayList list; 

Java will check to see if computeArea has been overridden. If it has it will use that version, else it will use the inherited version. e.g.

class Parent { void callMe(){ System.out.println("Parent"); } } class Child { void callMe(){ System.out.println("Child"); } } Child c = new Child(); c.callMe(); //will display Child 

It would call the Parent version, which would print Parent, but we overrode the method. That's basic overriding. Java also has polymorphism:

Parent p = new Child(); p.callMe(); //will display Child 

A reference of type Parent can refer to an instance of Child.

If you call a method of Parent that's been overridden by Child Java knows to call the Child's instance method, not the Parent's.

A little advanced, but that will really become useful in more advanced design methods, like "coding to interfaces".

If you are using the generic version of the list implementation, you don't need to cast, e.g.

ArrayList<BestTutor> list = null; BestTutor c = list.get(0); 

Generics is a type-safety method that tells Java nothing other than a BestTutor will go into this collection, so you can always bet that List.get() will return a BestTutor or whatever the bounded object is. BestTutor is called the bounded object. If you don't use generics the bounded object is Object., e.g.

ArrayList<Object> list; 

Although, this bounding is implicit, so it's just:

ArrayList list; 

Java will check to see if computeArea has been overridden. If it has it will use that version, else it will use the inherited version. e.g.

class Parent { void callMe(){ System.out.println("Parent"); } } class Child { void callMe(){ System.out.println("Child"); } } Child c = new Child(); c.callMe(); //will display Child 

It would call the Parent version, which would print Parent, but we overrode the method. That's basic overriding. Java also has polymorphism:

Parent p = new Child(); p.callMe(); //will display Child 

A reference of type Parent can refer to an instance of Child.

If you call a method of Parent that's been overridden by Child Java knows to call the Child's instance method, not the Parent's.

A little advanced, but that will really become useful in more advanced design methods, like "coding to interfaces".

Credit goes to Blackcompe

If you are using the generic version of the list implementation, you don't need to cast, e.g.

ArrayList<BestTutor> list = null; BestTutor c = list.get(0); 

Generics is a type-safety method that tells Java nothing other than a BestTutor will go into this collection, so you can always bet that List.get() will return a BestTutor or whatever the bounded object is. BestTutor is called the bounded object. If you don't use generics the bounded object is Object., e.g.

ArrayList<Object> list; 

Although, this bounding is implicit, so it's just:

ArrayList list; 

Java will check to see if computeArea has been overridden. If it has it will use that version, else it will use the inherited version. e.g.

class Parent { void callMe(){ System.out.println("Parent"); } } class Child { void callMe(){ System.out.println("Child"); } } Child c = new Child(); c.callMe(); //will display Child 

It would call the Parent version, which would print Parent, but we overrode the method. That's basic overriding. Java also has polymorphism:

Parent p = new Child(); p.callMe(); //will display Child 

A reference of type Parent can refer to an instance of Child.

If you call a method of Parent that's been overridden by Child Java knows to call the Child's instance method, not the Parent's.

A little advanced, but that will really become useful in more advanced design methods, like "coding to interfaces".

Post Undeleted by SkyWalker
better understanding and view
Source Link
SkyWalker
  • 29.3k
  • 14
  • 77
  • 134

HereIf you are using the generic version of the list implementation, you don't need to cast, e.g.

ArrayList<BestTutor> list = null; BestTutor c = list.get(0); 

instanceofGenerics operator comesis a type-safety method that tells Java nothing other than a BestTutor will go into use. Ifthis collection, so you want a data store with multiple typescan always bet that it is good to use the abstract class as the genericList.get() will return a BestTutor or whatever the bounded object is. BestTutor is called the bounded object. If you don't use generics the bounded object is Object., e.g.

abstract class CarpetArrayList<Object> {list; } 

Although, this bounding is implicit, so it's just:

ArrayList list; 

Java will check to see if computeArea has been overridden. If it has it will use that version, else it will use the inherited version. e.g.

class CircleCarpet extends CarpetParent {  } class RectCarpet extends Carpet {void  } callMe(){ ArrayList<Carpet> carpets = new ArrayList<Carpet>();  carpets.add(new CircleCarpet());  carpets.add(new RectCarpetSystem.out.println()"Parent"); ...  carpets.add( new CircleCarpet() );} ...} for(class CarpetChild c{  : carpets ) {void callMe(){ if( c instanceof CircleCarpet System.out.println("Child") ; } }  Child c = System.out.printlinenew Child();   c.getRadiuscallMe() );  }//will display Child 

Note: thatIt would call the is vital. Without itParent version, you must cast to the carpet you want -- otherwise you get the hashcode ofwhich would print Parent, but we overrode the objectmethod. That's basic overriding. Java also has polymorphism:

ArrayListParent carpetsp = new new ArrayListChild(); for( Carpet c : carpets ) { if( c instanceof CircleCarpet ) {   CircleCarpet cc = (CircleCarpet)c; ccp.getRadiuscallMe();  //will } display }Child 

A reference of type Parent can refer to an instance of Child.

If you call a method of Parent that's been overridden by Child Java knows to call the Child's instance method, not the Parent's.

A little advanced, but that will really become useful in more advanced design methods, like "coding to interfaces".

Here the instanceof operator comes into use. If you want a data store with multiple types it is good to use the abstract class as the generic.

abstract class Carpet { }  class CircleCarpet extends Carpet {  } class RectCarpet extends Carpet {  }  ArrayList<Carpet> carpets = new ArrayList<Carpet>();  carpets.add(new CircleCarpet());  carpets.add(new RectCarpet()); ...  carpets.add( new CircleCarpet() ); ... for( Carpet c : carpets ) { if( c instanceof CircleCarpet )  System.out.printline( c.getRadius() );  } 

Note: that the is vital. Without it, you must cast to the carpet you want -- otherwise you get the hashcode of the object.

ArrayList carpets = new new ArrayList(); for( Carpet c : carpets ) { if( c instanceof CircleCarpet ) {   CircleCarpet cc = (CircleCarpet)c; cc.getRadius();  }  } 

If you are using the generic version of the list implementation, you don't need to cast, e.g.

ArrayList<BestTutor> list = null; BestTutor c = list.get(0); 

Generics is a type-safety method that tells Java nothing other than a BestTutor will go into this collection, so you can always bet that List.get() will return a BestTutor or whatever the bounded object is. BestTutor is called the bounded object. If you don't use generics the bounded object is Object., e.g.

ArrayList<Object> list; 

Although, this bounding is implicit, so it's just:

ArrayList list; 

Java will check to see if computeArea has been overridden. If it has it will use that version, else it will use the inherited version. e.g.

class Parent { void callMe(){ System.out.println("Parent"); } } class Child {  void callMe(){ System.out.println("Child"); } }  Child c = new Child();   c.callMe(); //will display Child 

It would call the Parent version, which would print Parent, but we overrode the method. That's basic overriding. Java also has polymorphism:

Parent p = new Child(); p.callMe(); //will display Child 

A reference of type Parent can refer to an instance of Child.

If you call a method of Parent that's been overridden by Child Java knows to call the Child's instance method, not the Parent's.

A little advanced, but that will really become useful in more advanced design methods, like "coding to interfaces".

Post Deleted by SkyWalker
Source Link
SkyWalker
  • 29.3k
  • 14
  • 77
  • 134

Here the instanceof operator comes into use. If you want a data store with multiple types it is good to use the abstract class as the generic.

abstract class Carpet { } class CircleCarpet extends Carpet { } class RectCarpet extends Carpet { } ArrayList<Carpet> carpets = new ArrayList<Carpet>(); carpets.add(new CircleCarpet()); carpets.add(new RectCarpet()); ... carpets.add( new CircleCarpet() ); ... for( Carpet c : carpets ) { if( c instanceof CircleCarpet ) System.out.printline( c.getRadius() ); } 

Note: that the is vital. Without it, you must cast to the carpet you want -- otherwise you get the hashcode of the object.

ArrayList carpets = new new ArrayList(); for( Carpet c : carpets ) { if( c instanceof CircleCarpet ) { CircleCarpet cc = (CircleCarpet)c; cc.getRadius(); } }