Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

what is super in java

// to access a property 'num' from SuperClass:
super.num //or
((SuperClass)this).num
// to run the constructor of SuperClass: 
super()
Comment

Java super Keyword

class Animal { // Superclass (parent)
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Dog extends Animal { // Subclass (child)
  public void animalSound() {
    super.animalSound(); // Call the superclass method
    System.out.println("The dog says: bow wow");
  }
}

public class Main {
  public static void main(String args[]) {
    Animal myDog = new Dog(); // Create a Dog object
    myDog.animalSound(); // Call the method on the Dog object
  }
}
Comment

using super in Java

class Shape {
	public void draw() {
		System.out.println("Shape draw.");
	}
}
class Circle extends Shape {
	@Override
	public void draw() {
		System.out.println("Circle draw.");
		super.draw(); // Access parent's version of draw
	}
}
public class Main {
	public static void main(String[] args) {
		Circle c = new Circle();
		// The below prints: 
		// Circle draw.
		// Shape draw.
		c.draw();
	}	
}
Comment

Java Use of super Keyword

class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   public void displayInfo() {
      super.displayInfo();
      System.out.println("I am a dog.");
   }
}

class Main {
   public static void main(String[] args) {
      Dog d1 = new Dog();
      d1.displayInfo();
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Iterating through LinkedList 
Java :: convert character arraylist to array 
Java :: java hashmap 
Java :: singleton 
Java :: java long literal 
Java :: matrix dimensions 
Java :: how to divide a double in java 
Java :: generics in java 
Java :: java programming problems 
Java :: instantiation in java 
Java :: abstract class java 
Java :: Java TestNG Data Provider example 
Java :: Is the main method compulsory in Java? 
Java :: array 2 
Java :: Multi basic auth with spring security 
Java :: h2 database spring boot create table application.properties 
Java :: Develop the Java application called Shapes. For this program, use only for loops and the following print statements below to generate the shapes below: 
Java :: access char in string 
Java :: key caracter from code java 
Java :: how to pass parameters to xsl file 
Java :: textfield invisible java 
Java :: Using Looping Construct to Copy Arrays Java 
Java :: calculate tip and sales tax function 
Java :: Start Text from top left android 
Java :: printing array in descending order 
Java :: success listener with Glide Android java 
Java :: charstreams cannot be resolved 
Java :: java code to implement hybrid interface 
Java :: resources/android/xml/network_security_config.xml 
Java :: Changing or Replacing Elements in java map 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =