Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java inheritance


public class Sample {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        Duck duck = new Duck();
        System.out.println(dog.getSound());
        System.out.println(cat.getSound());
        System.out.println(duck.getSound());
    }
}

class Animal {
    private String sound = "anything";

    public void setSound(String sound) {
        this.sound = sound;
    }

    public String getSound() {
        return sound;
    }
}

class Dog extends Animal {
    private String sound = "Aw aw";

    public String getSound() {
        return sound;
    }
}

class Cat extends Animal {
    private String sound = "Meow meow";

    public String getSound() {
        return sound;
    }
}

class Duck extends Animal {
    private String sound = "Quack Quack";

    public String getSound() {
        return sound;
    }
}
Comment

inheritance in java

Inheritance in Java is a mechanism in which one object acquires 
all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
Comment

Inheritance in Java

Java Inheritance (Subclass and Superclass)
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
Comment

Java Inheritance

class Vehicle {
  protected String brand = "Ford";        // Vehicle attribute
  public void honk() {                    // Vehicle method
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle {
  private String modelName = "Mustang";    // Car attribute
  public static void main(String[] args) {

    // Create a myCar object
    Car myCar = new Car();

    // Call the honk() method (from the Vehicle class) on the myCar object
    myCar.honk();

    // Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
    System.out.println(myCar.brand + " " + myCar.modelName);
  }
}
Comment

java inheritance example

public class Parent
{
  //methods and constructors 
}

public class Child extends Parent
{
	//inherits methods from Parent class
}
Comment

what is inheritance in java

Inheritance is the mechanism where the object of one class acquires the property of the object of another class.
Comment

Inheritance in java

Super class:
public class Multi(){
}
Sub class:
public class Multiplication extends Maltil(){
}
Comment

PREVIOUS NEXT
Code Example
Java :: java display message 
Java :: java sort a Map by keys reverse descending order 
Java :: crear objetos automaticamente java 
Java :: how to use user uid in android 
Java :: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification 
Java :: how to substring in java 
Java :: java find if element of list in present in another list 
Java :: @crossorigin spring allow all 
Java :: android java get value from listview item 
Java :: how to split string in java android 
Java :: Add month to date in java 8 
Java :: pre increment and post increments java 
Java :: char to ascii java 
Java :: how to get current time and date in android 
Java :: java get jar execution directory 
Java :: spring boot dockerfile 
Java :: what is transient in java 
Java :: convert java to c# 
Java :: How to check if a string is in alphabetical order in java 
Java :: convert array to string in java 
Java :: button change text java 
Java :: breadth first search bst java 
Java :: how to change the character of a string in java 
Java :: Divide two integers without using multiplication, division and mod operator 
Java :: java boucle for 
Java :: declare generic set java 
Java :: java set 
Java :: main method in java 
Java :: if checkbox checked java 
Java :: android convert date to local timezone 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =