diff --git a/06_Arrays/arrays.md b/06_Arrays/arrays.md index fc8cea3..02c95b0 100644 --- a/06_Arrays/arrays.md +++ b/06_Arrays/arrays.md @@ -558,7 +558,81 @@ 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 + 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? +} +}