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 :: how to iterate hashmap in java 
Java :: jpa repository delete method return value 
Java :: check if object in array java 
Java :: kotlin android intent pass data 
Java :: edittext color 
Java :: sum of array recursion java 
Java :: java string array add element 
Java :: close scanner java 
Java :: java arraylist get second largest value 
Java :: check type of variable java 
Java :: java generic array 
Java :: list java initialize 
Java :: java coding problems 
Java :: java shorthand if 
Java :: check if jcheckbox is checked java 
Java :: how to find a specific word in a text file in java 
Java :: find duplicate value in array java 
Java :: what is the difference between sc.nextLine() and sc.next() in java 
Java :: duplicate local variable in java 
Java :: array to list 
Java :: ++i vs i++ java 
Java :: using buidfeatures to enable viewbinding 
Java :: console printing in java 
Java :: offsetdattime to date 
Java :: java outer class 
Java :: font parameters java 
Java :: java generate secure random password 
Java :: write json file java 
Java :: iterating over a hashmap 
Java :: how to create an array in java 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =