Java Tutorial For Beginners 25 – Inheritance in Java

Java

Video is ready, Click Here to View ×


java inheritance constructor
java inheritance vs interface
java inheritance static variables
java inheritance static methods
java vererbung
java subclass
java extend class
java class declarationjava inheritance example
java inheritance example program
java polymorphism
java inheritance constructor
java multiple inheritance
java inheritance interview questions
java inheritance pdf
java inheritance definition

41 thoughts on “Java Tutorial For Beginners 25 – Inheritance in Java

  1. User interface program in same method

    public class MyProgram{
    public static void main(String[] args) {
    rectangle rect =new rectangle();
    triangle triag=new triangle();
    rect.valset();
    triag.valset();
    System.out.println("The Area of Rectangle is: " + rect.Area());
    System.out.println("The area of triangle is: " + triag.Area());
    }
    }

    import java.util.Scanner;
    public class polygon {
    protected int length;
    protected int breadth;
    public void valset() {
    int l;
    int h;
    System.out.println("Please enter Length: ");
    Scanner a=new Scanner(System.in);
    l = a.nextInt();
    System.out.println("Please enter breadth or height: ");
    Scanner b =new Scanner(System.in);
    h =b.nextInt();
    length = l;
    breadth = h;
    }
    }

    public class rectangle extends polygon {
    public double Area() {
    return(length*breadth);
    }
    }

    public class triangle extends polygon {
    public double Area() {
    return(length*breadth)/2;
    }
    }

  2. I thought my headphones were broken, so then I went on a 10-minute search to find other headphones, then realized they were broken too. Then I read the comments. Lesson: read the comments first.

  3. Hi Guys.
    Does anyone know how to use a value from other Method (e.g public class int calculate() {})?
    How should i use the variable that was set in another method or class?
    Thanks all!

  4. In inheritance you can take the variable and method for other class and difference between interface is necessary use abstract you can only take the method, I´m right?

  5. Hi ProgrammingKnowledge,

    While writing the code of inheritance I declared the method to set values in polygon as static and now I am getting warning at the variables being set as "can not make static reference to non static field height"

    Please see the code written by me for Polygon class:

    public class Polygon {

    protected int height;
    protected int width;

    public static void setValues(int l,int w)
    {
    height= l; // warning "can not make static reference to non static field height"
    width= w; // warning "can not make static reference to non static field width"

    }

    Once I remove the "static" keyword from setValues method the error is gone.

  6. There's a small issue in the area() in Rectangle class. You must cast the height*width to (double) before dividing by 2.
    Here by chance the result was true. If it were say 50.xx it'll be also 50. Since it's applying "Integer division" & then, returning it as a double.

  7. Get creative with it. Only a few hours in and I feel like I am learning quite a bit. Try getting user input for height and width like this one:

    public static void main(String[] args) {

    Rectangle rec = new Rectangle();

    System.out.println("Area of a Rectangle");
    System.out.println(" —————–");
    System.out.println("| |");
    System.out.println("| |");
    System.out.println("| |");
    System.out.println(" —————–");
    System.out.println("Enter height of Rectangle");
    Scanner scan = new Scanner(System.in);
    double user_input_rec_height = scan.nextDouble();

    System.out.println("Enter width of Rectangle");
    Scanner scan1 = new Scanner(System.in);
    double user_input_rec_width = scan.nextDouble();

    rec.height = user_input_rec_height;
    rec.width = user_input_rec_width;
    System.out.println("The area of the rectangle is " +rec.area());
    System.out.println("-—————————");

    Triangle tri = new Triangle();
    System.out.println("Area of a Triangle");
    System.out.println(" <>");
    System.out.println(" [][]");
    System.out.println(" [] []");
    System.out.println(" [] []");
    System.out.println(" [] []");
    System.out.println(" [] []");
    System.out.println(" [][][][][][][]");
    System.out.println("Enter height of Triangle");
    Scanner scan2 = new Scanner(System.in);
    double user_input_tri_height = scan.nextDouble();

    System.out.println("Enter width of Triangle");
    Scanner scan3 = new Scanner(System.in);
    double user_input_tri_width = scan.nextDouble();

    tri.height = user_input_tri_height;
    tri.width = user_input_tri_width;
    System.out.println("The area of the rectangle is " +tri.area());

  8. Thanks for the videos , very helpful , just few comments.

    your voice is not clear not only because the left earphone error , you're far away from Microphone or it's not a good one.

    Also you repeat your self many times. sometimes it's helpful and other times it's very irritating.

    hope this series will improve and show us real java not only methods and calculations.

    we haven't any interface or display window.

    But in total it's a very helpful series and thanks for uploading this and share with us your experiences

  9. Do you hear the chirp sound every 37 seconds? Yes that is your smoke detector. It has a dead battery. Replace the battery…. jesus christ.

    Also, most of your video is in mono audio. What that means is that your voice only comes out of one speaker and static comes out of the other one. It is highly annoying, fix it.

  10. I don't know why but this is only audible in right ear but not left,first I thought it's my earpiece fault,but it's working fine,please correct the audio settings of your video

Leave a Reply

Your email address will not be published. Required fields are marked *