~~ CORE-JAVA ~~

 1. INTORDUCTION

     1.1 Java Intorduction
     1.2 Java history
     1.3 Java features
     1.4 Diff b/w Java & C++
     1.5 Java Variables
     1.6 Java Data Types
     1.7 Typecasting
     1.8 Arrays

 2. OOPS

     2.1 Introduction
     2.2 Class
     2.3 Object
     2.4 Static Keywords
     2.5 Constructors
     2.6 This Key Word

 3. INHERITANCE

     3.1 Inheritance (IS-A) 
     3.2 Aggregation (HAS-A)

 4. POLYMORPHISM

     4.1 Polymorphism & Its Type
     4.2 Method Overloading
     4.3 Method Overriding
     4.4 Super Keyword
     4.5 Final Keyword
     4.6 Dynamic Binding

 5. ABSTRACTION

     5.1 Abstract Classes & Methods
     5.2 Interfaces
     5.3 Abstract vs Interface

 6. ENCAPSULATION

     6.1 Encapsulation

 7. STRING MANIPULATIONS

     7.1 String
     7.2 String Buffer
     7.3 String Tokenizer

 8. PACKAGES

     8.1 Predefined packages
     8.2 Userdefined Packages
     8.3 Access Specifiers

 9. EXCEPTION HANDLING

     9.1 Introduction
     9.2 Pre Defined Exceptions
     9.3 Try-Catch-Finally
     9.4 Throws, throw
     9.5 User Defined Exception

 10. MULTITHREADING

     10.1 Introduction
     10.2 Thread Creations
     10.3 Thread Life Cycle
     10.4 Life Cycle Methods
     10.5 Synchronization
     10.6 Wait() notify() notify all()

 11. WRAPPERCLASSES

     11.1 Introduction
     11.2 Wrapper Classes

 12. COLLECTION FRAME WORK

     12.1 Introduction
     12.2 List interface
     12.3 Set interface                                 12.4 Map interface                           

 13. INNER CLASSES

     13.1 Introduction
     13.2 Member inner class
     13.3 Static inner class
     13.4 Local inner class
     13.5 Anonymous inner class

 14. CoreJava Interview Questions

Java | Inheritance (IS-A) :

  • When a Class or Object inherit all the properties of Other Class or Object, then it’s nothing but Inheritance only. It’s very important feature of OOPS.
  • In Java, IS-A Relationship and Inheritance both are same.
  • The main advantage of Inheritance is Re-usability of the Code.
  • By using extends Keyword we can achieve Inheritance or IS-A Relationship.

Syntax :

class Human 
{
......
}

//Man class is extending property of Human class
class Man extends Human
{
......
}

 

Super Class / Parent Class :

Super Class are these Classes, which is inherited by Sub Class or Child Class. Super Class also known as Parent Class. In above example Human is a Super Class. 

Sub Class / Child Class :

Sub Class are these Classes, which is inheriting some other Class properties, is nothing but Sub Class. Sub Class also known as Child Class. In above example Man is a Sub Class.

Notes :

  • Whatever the Parent Class has by default it will be available to the Child Class. So here Child Class reference can call both Parent and Child class method.
  • Whatever the Child Class has by default it will not available to Parent Class. So here Parent Class reference can call only Parent Class method not Child Class method.
  • Here we can use Parent Class reference to hold Child Class Objects, but by using that reference we can call only Parent Class methods, not Child Class methods.
  • We can’t use Child Class reference to hold Parent Class Object.
  • Also here we can say Parent Class and Child Class both are tightly coupled, because here we can see Child Class is depend on Parent Class. Whenever in Parent Class some changes will be there, it will reflect of Child Class also. which is it’s main disadvantage.

 

Types Of Inheritance :

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance

Note : Java doesn’t support Multiple Inheritance, It’s supporting multiple through Interface only. we will see Interface and why it’s not supporting.

1.Single Inheritance :

When a Class inherit, Other Class then it’s nothing but Single Inheritance only. Below we will see simple example of Single Inheritance, where we have AdvCalculator Class which will use property of Calculator Class.

Example :

class Calculator {
public int add(int i, int j) {
return i + j;
}
}

//extending property of Calculator class
class AdvCalculator extends Calculator {
public int sub(int i, int j) {
return i – j;
}
}

class InheritanceDemo {
public static void main(String[] args) {
AdvCalculator acalc = new AdvCalculator();
int add = acalc.add(5, 3);
int sub = acalc.sub(5, 3);

System.out.println(“Addition is : ” + add + ” and Subtraction is : ” + sub);
}
}

Output :

Addition is : 8 and Subtraction is : 2

Note : Here in above Single Inheritance example, we can see AdvCalculator is extending Calculator Class and we are creating Object of AdvCalculator (child class). With this object we are calling both method add() and sub(). In short we can say here AdvCalculator Class is using feature of Calculator Class also.

2.Multilevel Inheritance :

When a Class extends another Class, and then again some other class extends that class, then such type of Inheritance we can call Multilevel Inheritance. We will see below example where AdvCalculator Class is inheriting Calculator Class and then again VeryAdvCalculator is inveriting AdvCalculator.

Example :

class Calculator {
public int add(int i, int j) {
return i + j;
}
}

//AdvCalculator is inheriting Calculator Class
class AdvCalculator extends Calculator {
public int sub(int i, int j) {
return i – j;
}
}

//VeryAdvCalculator is inhering AdvCalculator Class
class VeryAdvCalculator extends AdvCalculator {
public int mul(int i, int j) {
return i * j;
}
}

class InheritanceDemo {
public static void main(String[] args) {

//here we are creating obj of Child Class and calling all these methods.

VeryAdvCalculator calc = new VeryAdvCalculator();
int add = calc.add(5, 3);
int sub = calc.sub(5, 3);
int mul = calc.mul(5, 3);

System.out.println(“Addition is : ” + add);
System.out.println(“Subtraction is : ” + sub);
System.out.println(“Multiplication is : ” + mul);
}
}

Output :

Addition is : 8
Subtraction is : 2
Multiplication is : 15

Note : Here in above Multilevel example we can see we are creating object of child class (VeryAdvCalculator) and we are able to call all these methods.

2.Hierarchical Inheritance :

When two or more classes inherit a single class, then it’s nothing but Hierarchical Inheritance. In below example we will see how we can achieve this.

Example :

class Calculator {
public int add(int i, int j) {
return i + j;
}
}

//AdvCalculator is inhereting Calculator Class
class AdvCalculator extends Calculator {
public int sub(int i, int j) {
return i – j;
}
}

//VeryAdvCalculator is also inhereting Calculator Class only
class VeryAdvCalculator extends Calculator {
public int mul(int i, int j) {
return i * j;
}
}

class InheritanceDemo {
public static void main(String[] args) {

//Here we will create Obj of VeryAdvCalculator and will call both method add() and mul()
VeryAdvCalculator calc = new VeryAdvCalculator();
int add = calc.add(5, 3);
int mul = calc.mul(5, 3);

System.out.println(“Addition is : ” + add);
System.out.println(“Multiplication is : ” + mul);
}
}

Output :

Addition is : 8
Multiplication is : 15

Note : Here in above Hierarchical Inheritance example, we can see two classes (AdvCalculator and VeryAdvCalculator) are inheriting single class (Calculator).

Multiple Inheritance is not supported by Java :

dsdsdsdd

Help Others, Please Share