From a526202fabce07501e9d1b6aaecd0ff037e3a0b9 Mon Sep 17 00:00:00 2001 From: Aliza Dixit Date: Wed, 11 Oct 2023 01:06:08 +0530 Subject: [PATCH 1/4] Updated new arrays.md Signed-off-by: Aliza Dixit --- 06_Arrays/arrays.md | 72 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/06_Arrays/arrays.md b/06_Arrays/arrays.md index fc8cea3..936bb9d 100644 --- a/06_Arrays/arrays.md +++ b/06_Arrays/arrays.md @@ -558,7 +558,77 @@ true ``` -# + + + +##deep copy and shalllow copy + +lets talk about shallow copy and deep copy over here in arrays + +#In Shallow copy, a copy of the original object is stored and only the reference address is finally copied. In Deep copy, the copy of the original object and the repetitive copies both are stored + +code for shallow copy and deep copy: +import java.util.ArrayList; + +// Class of Car +class Car { + public String name; + public ArrayList colors; + + public Car(String name, ArrayList colors) + { + this.name = name; + this.colors = colors; + } +} + +public class Main { + public static void main(String[] args) + { + // Create a Honda car object + ArrayList hondaColors = new ArrayList<>(); + hondaColors.add("Red"); + hondaColors.add("Blue"); + Car honda = new Car("Honda", hondaColors); + + // Deep copy of Honda + Car deepcopyHonda = new Car( + honda.name, new ArrayList<>(honda.colors)); + deepcopyHonda.colors.add("Green"); + System.out.print("Deepcopy: "); + for (String color : deepcopyHonda.colors) { + System.out.print(color + " "); + } + System.out.println("\nOriginal: "); + for (String color : honda.colors) { + System.out.print(color + " "); + } + System.out.println(); + + // Shallow Copy of Honda + Car copyHonda = honda; + copyHonda.colors.add("Green"); + System.out.print("Shallow Copy: "); + for (String color : copyHonda.colors) { + System.out.print(color + " "); + } + System.out.println("\nOriginal: "); + for (String color : honda.colors) { + System.out.print(color + " "); + } + System.out.println(); + } +} +output: +Deepcopy: ['Red', 'Blue', 'Green'] +Original: ['Red', 'Blue'] +Shallow Copy: ['Red', 'Blue', 'Green'] +Original: ['Red', 'Blue', 'Green'] + + [Previous](../05_Loops/loops.md) Loops [Next](../07_Strings/strings.md) Strings + +lets talk about shallow copy and deep copy over here in arrays + From 85d0e0e94fbbd959f5be64f70f70e2fad82649fa Mon Sep 17 00:00:00 2001 From: Aliza Dixit Date: Wed, 11 Oct 2023 01:08:40 +0530 Subject: [PATCH 2/4] Update arrays.md Signed-off-by: Aliza Dixit --- 06_Arrays/arrays.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/06_Arrays/arrays.md b/06_Arrays/arrays.md index 936bb9d..02c95b0 100644 --- a/06_Arrays/arrays.md +++ b/06_Arrays/arrays.md @@ -568,9 +568,13 @@ lets talk about shallow copy and deep copy over here in arrays #In Shallow copy, a copy of the original object is stored and only the reference address is finally copied. In Deep copy, the copy of the original object and the repetitive copies both are stored code for shallow copy and deep copy: + + + import java.util.ArrayList; // Class of Car + class Car { public String name; public ArrayList colors; @@ -619,6 +623,8 @@ public class Main { System.out.println(); } } + + output: Deepcopy: ['Red', 'Blue', 'Green'] Original: ['Red', 'Blue'] @@ -630,5 +636,3 @@ Original: ['Red', 'Blue', 'Green'] [Next](../07_Strings/strings.md) Strings -lets talk about shallow copy and deep copy over here in arrays - From 33050b04c8e33cb08d1edf65027bc0e6f6e069c1 Mon Sep 17 00:00:00 2001 From: Aliza Dixit Date: Wed, 11 Oct 2023 01:21:10 +0530 Subject: [PATCH 3/4] Update inheritance.md Signed-off-by: Aliza Dixit --- 10_Inheritance/inheritance.md | 90 +++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/10_Inheritance/inheritance.md b/10_Inheritance/inheritance.md index 8b13789..4f89284 100644 --- a/10_Inheritance/inheritance.md +++ b/10_Inheritance/inheritance.md @@ -1 +1,91 @@ +## in this i will show how inheritance works +In java 4 types of inheritance is possible by normal class and the one which is not possible by normal class is done by intrefaces +The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. +##Single Inheritance Example +When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance. + +File: TestInheritance.java + +class Animal{ +void eat(){System.out.println("eating...");} +} +class Dog extends Animal{ +void bark(){System.out.println("barking...");} +} +class TestInheritance{ +public static void main(String args[]){ +Dog d=new Dog(); +d.bark(); +d.eat(); +}} + +##Multilevel Inheritance Example +When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance. + +File: TestInheritance2.java + +class Animal{ +void eat(){System.out.println("eating...");} +} +class Dog extends Animal{ +void bark(){System.out.println("barking...");} +} +class BabyDog extends Dog{ +void weep(){System.out.println("weeping...");} +} +class TestInheritance2{ +public static void main(String args[]){ +BabyDog d=new BabyDog(); +d.weep(); +d.bark(); +d.eat(); +}} + +##Hierarchical Inheritance Example +When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance. + +File: TestInheritance3.java + +class Animal{ +void eat(){System.out.println("eating...");} +} +class Dog extends Animal{ +void bark(){System.out.println("barking...");} +} +class Cat extends Animal{ +void meow(){System.out.println("meowing...");} +} +class TestInheritance3{ +public static void main(String args[]){ +Cat c=new Cat(); +c.meow(); +c.eat(); +//c.bark();//C.T.Error +}} + + + +## multilevel inheritance by interface + +Q) Why multiple inheritance is not supported in java? +To reduce the complexity and simplify the language, multiple inheritance is not supported in java. + +Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. + +Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error. + + +class A{ +void msg(){System.out.println("Hello");} +} +class B{ +void msg(){System.out.println("Welcome");} +} +class C extends A,B{//suppose if it were + + public static void main(String args[]){ + C obj=new C(); + obj.msg();//Now which msg() method would be invoked? +} +} From e20684be102b1dcee00c71d3ecacd4f9cbcc9555 Mon Sep 17 00:00:00 2001 From: Aliza Dixit Date: Wed, 11 Oct 2023 22:00:16 +0530 Subject: [PATCH 4/4] Update inheritance.md done Signed-off-by: Aliza Dixit