Day 2 starts recollect the previews day class. After that start the next topic:
** INHERITANCE:**
* Java Inheritance is a fundamental concept in object-oriented programming that allows a new class to inherit properties and behaviors (fields and methods) from an existing class.
* To inherit in Java means allowing all methods and variables from one class to be accessible by another class. That is, the new class inherits these items. The parent class, also called superclass, is the class whose methods and variables can be used in the child class (also called subclass)
TYPES OF INHERITANCE:
-
Single Inheritance:
* A subclass or parentclass inherits from only one superclass or childclass. * one parent class only childclass
SYNTAX:
class Superclass {
// Superclass members
}
class Subclass extends Superclass {
// Subclass members
}
2.Multilevel Inheritance:
* A class inherits from another class, which in turn inherits from another class, forming a chain of inheritance.
* 1-parentclass --- 2-childclass ----2-childclass convented to parented class of the threed class ----3-childclass(ex : greand father-kannan (parent of his son kumar),kumar is a child of kannan,and then lokesh is a child of kumar. so this time kumar act as a parent .
SYNTAX
class Grandparent {
// Grandparent members
}
class Parent extends Grandparent {
// Parent members
}
class Child extends Parent {
// Child members
}
3.Hierarchical Inheritance:
* Multiple subclasses inherit from a single superclass.
* one parentclass -- multiple child class .
SYNTAX
class Superclass {
// Superclass members
}
class Subclass1 extends Superclass {
// Subclass1 members
}
class Subclass2 extends Superclass {
// Subclass2 members
}
4.Multiple Inheritance:
* A class inherits from multiple superclasses. Java doesn't directly support multiple inheritance with classes but achieves it through interfaces.
SYNTAX
interface Interface1 {
// Interface1 methods
}
interface Interface2 {
// Interface2 methods
}
class MyClass implements Interface1, Interface2 {
// Class members and implementation of interface methods
}
5.Hybrid Inheritance:
* A combination of two or more types of inheritance. In Java, this is achieved using interfaces and classes.
* Two parent class one child class . It's like a father,mother, and one son.
SYNTAX
interface Interface1 {
// Interface1 methods
}
class Superclass {
// Superclass members
}
class Subclass extends Superclass implements Interface1 {
// Subclass members
}
MAIN ADVANTAGES OF INHERITANCE:
- Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces.
Top comments (0)