Добавить
Уведомления

Java - Day 70 (in Telugu) - interface

/* class Fruit{ void size() { System.out.println("big"); } void color() { System.out.println("green"); } } abstract class Fruit{ abstract void size(); abstract void color(); } */ interface Fruit{ void size(); // public abstract void size(); void color(); // public abstract void color(); int a = 5; // public static final int a; int b = 6; // public static final int b; } class Mango implements Fruit{ public void size() { System.out.println("medium"); } public void color() { System.out.println("yellow"); } } class Grape implements Fruit{ public void size() { System.out.println("smaller"); } public void color() { System.out.println("white"); } } public class Inter{ public static void main(String arg[]){ Fruit f; f = new Mango(); f.size(); f.color(); f = new Grape(); f.size(); f.color(); // f = new Fruit(); Fruit is interface. We cannot create object off it. } } /* When we need code inside the methods of parent class? 1) if we need an object of the parent class and need to invoke methods through it. 2) if we need a common code in all the child classes If we want to make a method as abstract in an abstract class, then we have to exclusively mention "abstract" before that method. A child class "extends" a parent class. A child class "extends" an parent abstract class. We can create objects off a class. We cannot create objects off an abstract class. We can have instance variables in a class. We can have instance variables in an abstract class. Interface is similar to an abstract class. By default, all methods in an interface are "public" and "abstract". So, the child classes should use "public" before the method while implementing/overriding. A child class "implements" a parent interface. We cannot create objects off an interface. We cannot have instance variables in an interface. Variables created inside an interface are 'static' by default. Variables created inside an interface are 'public' by default. Variables created inside an interface are 'final' by default. As variables in an interface are final, they should be initialized at the declaration. */

12+
15 просмотров
2 года назад
12+
15 просмотров
2 года назад

/* class Fruit{ void size() { System.out.println("big"); } void color() { System.out.println("green"); } } abstract class Fruit{ abstract void size(); abstract void color(); } */ interface Fruit{ void size(); // public abstract void size(); void color(); // public abstract void color(); int a = 5; // public static final int a; int b = 6; // public static final int b; } class Mango implements Fruit{ public void size() { System.out.println("medium"); } public void color() { System.out.println("yellow"); } } class Grape implements Fruit{ public void size() { System.out.println("smaller"); } public void color() { System.out.println("white"); } } public class Inter{ public static void main(String arg[]){ Fruit f; f = new Mango(); f.size(); f.color(); f = new Grape(); f.size(); f.color(); // f = new Fruit(); Fruit is interface. We cannot create object off it. } } /* When we need code inside the methods of parent class? 1) if we need an object of the parent class and need to invoke methods through it. 2) if we need a common code in all the child classes If we want to make a method as abstract in an abstract class, then we have to exclusively mention "abstract" before that method. A child class "extends" a parent class. A child class "extends" an parent abstract class. We can create objects off a class. We cannot create objects off an abstract class. We can have instance variables in a class. We can have instance variables in an abstract class. Interface is similar to an abstract class. By default, all methods in an interface are "public" and "abstract". So, the child classes should use "public" before the method while implementing/overriding. A child class "implements" a parent interface. We cannot create objects off an interface. We cannot have instance variables in an interface. Variables created inside an interface are 'static' by default. Variables created inside an interface are 'public' by default. Variables created inside an interface are 'final' by default. As variables in an interface are final, they should be initialized at the declaration. */

, чтобы оставлять комментарии